joomla templating custom JDOC statements - joomla

Which Joomla Platform / CMS class should I extend in order to make my own custom JDOC:include tags?
I would like to have custom JDOC tags like
<JDOC:include type="scripts" />
<JDOC:include type="scripts-body" />
and a bunch of other types.

You need to add new file at below location to add custom jdoc tag..
libraries/joomla/document/html/renderer/
File name should be same as the tag you are adding.. suppose you want to use scripts as tag then file name should be scripts.php
Now in this file you need to add below code. as tag name is scripts so class name should be JDocumentRendererScripts
<?php
defined('JPATH_PLATFORM') or die;
class JDocumentRendererScripts extends JDocumentRenderer
{
public function render($scripts, $params = array(), $content = null)
{
$contents = "";
//Do your work here
return $contents;
}
}
?>
Now you can use custom jdoc code <JDOC:include type="scripts" />
Hope this helps..

Related

What's good way to load header and footer view in codeigniter?

I'm using codeigniter 3x. I'm working on my website. I'm using include method in my view.
Like
<?php include('templates/header.php'); ?>
<h1>Home Page</h1>
<?php include('templates/footer.php'); ?>
Is this a good way to show header and footer in codeigniter.
Thank!
You are half your way, here is how you will be able to make it more dynamic, in your views file you should have a structure like this:
views
- header.php
- footer.php
- template.php
- home.page
In header.php you should have all your header and footer content which you wants to display on all pages.
Now in your template move all your includes.
template.php
<?php $this->load->view("header.php"); ?>
<?php $this->load->view($main_content); ?>
<?php $this->load->view("footer.php"); ?>
here you notice $main_content variable, it is dynamic file name which we want to load in our controller. So lets assume you have a controller like this:
public function home()
{
$data['meta_title'] = $this->lang->line('home_meta_title');
$data['meta_description'] = $this->lang->line('home_meta_description');
$data['meta_keywords'] = $this->lang->line('home_meta_keywords');
$data['main_content'] = 'home';
$this->load->view('template',$data);
}
$data['main_content'] = 'home'; is loading your home.php file, you can also load from subdirectories like 'directory/home'. You can also pass any variable like I gave you above example with dynamic meta.

Laravel 5 How to dynamically include External CSS and JS file depends on controllers/routes

I'm wondering what could be a good way to
dynamically include a js file in my main blade
template like this
<?php
$currentRoute = Route::currentRouteName();
$currentRoute = $currentRoute?$currentRoute:'home';
$currentRoute = explode('.',$currentRoute);
?>
<script src="{{$currentRoute[0]}}.js"></script>
but just a less tricky :)
I did something like that in that past.
Here, is my "trick" to do this.
First you need a top level controller class
class MainController extends Controller {
protected static $js_files = [];
protected static $css_files = [];
public static function boot()
{
view()->share('js_files', static::$js_files);
view()->share('css_files', static::$css_files);
  parent::boot();
}
}
Then, all of your controllers must inherit from your MainController class.
The reason I use the keyword "static::" instead of "self::" is because I want to retrieve the latest child properties and not the "MainController" property, which would gave me the keywords "self::".
See http://php.net/manual/en/language.oop5.late-static-bindings.php
In your blade layout, in the head section do the following :
#foreach($css_files as $src)
<link href="{{$src}}" rel="stylesheet">
#endforeach
and before your closing body tag
#foreach($js_files as $src)
<script src="{{$src}}"></script>
#endforeach
In each controllers, you have to put in the $js_files and $css_files inherited properties the sources of all of your js/css.
if you want to include a JS/CSS only for one route, you can add the source of the file directly in the method.
And voilà !

Including SVG contents in Laravel 5 Blade template

What is the best way to include the contents of an SVG file (located in the assets folder) in a Laravel 5 blade template?
I don't want to use image/object/embed tags, this should be an inline SVG for reasons of speed.
I know I could use <?php file_get_contents("file.svg") ?> but is there a better way specific to Laravel/Blade?
Edit: to clarify, the method should work with all SVG files, including the one below.
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg">
<path stroke="red" fill="#00f" d="M10 10h100v100H10z"/>
</svg>
Similar to the accepted answer but a bit cleaner (imo).
Use the laravel directive to extend blade, like so (in your App Service Provider, as outlined here):
\Blade::directive('svg', function($arguments) {
// Funky madness to accept multiple arguments into the directive
list($path, $class) = array_pad(explode(',', trim($arguments, "() ")), 2, '');
$path = trim($path, "' ");
$class = trim($class, "' ");
// Create the dom document as per the other answers
$svg = new \DOMDocument();
$svg->load(public_path($path));
$svg->documentElement->setAttribute("class", $class);
$output = $svg->saveXML($svg->documentElement);
return $output;
});
Then use it in your blade like so:
<div class="Login__image Login__cloud">
#svg('cloud.svg', 'Cloud')
</div>
This works, that's the simplest way I could think about :
{!! file_get_contents('images/icon.svg') !!}
Why not place the svg into a blade template?
resources/views/icons/dashboard.blade.php
then add in your views using blade syntax?
#include('icons.dashboard')
View Composer Method
I ended up using a view composer in a service provider.
In the service provider's boot() method:
// Wildcard view composer
view()->composer('*', function($view) {
// Instantiate new DOMDocument object
$svg = new DOMDocument();
// Load SVG file from public folder
$svg->load(public_path('images/logo.svg'));
// Add CSS class (you can omit this line)
$svg->documentElement->setAttribute("class", "logo");
// Get XML without version element
$logo = $svg->saveXML($svg->documentElement);
// Attach data to view
$view->with('logo', $logo);
});
And in my view:
<!-- Echo unescaped SVG content -->
{!! $logo !!}
I am using DOMDocument as it allows me to remove the XML version element which should not be in the HTML.
The CSS class is not essential but saves me wrapping the logo with another HTML element for styling.
If you only need the logo in a specific Blade partial such as header you could write
view()->composer('header', function($view) {});
http://laravel.com/docs/5.0/views#view-composers
https://laracasts.com/series/laravel-5-fundamentals/episodes/25
Blade Partial Method
This method is not best practice since this sort of code should not really be in a view. However it is very simple and much better than adding PHP code in every single view.
Make a new partial (lets say logo.blade.php) with the following code:
<?php
// Instantiate new DOMDocument object
$svg = new DOMDocument();
// Load SVG file from public folder
$svg->load(public_path('images/logo.svg'));
// Add CSS class (you can omit this line)
$svg->documentElement->setAttribute("class", "logo");
// Echo XML without version element
echo $svg->saveXML($svg->documentElement);
?>
You can now use the SVG image in a blade template by including the partial like so:
#include('logo')

Custom HTML in Joomla Template Manager

While developing a Joomla Template, i got stuck on this question.
Every Joomla Module has a manifest. In that manifest you can set your Paramaters for your template with Field and Fieldset types. Is it possible to add a custom image in these sections? I tried CDATA for this but it doesn't work.
I've never tried this with a template, only a plugin and module, but I believe it's still the same concept.
Create a new folder in your template folder and name it "elements".
Then, in your template.xml file, find the <fieldset> that the parameter belongs to and add the following inside the tag:
addfieldpath="/modules/mod_social_slider/fields"
You now need to add a new parameter field like so:
<field name="image" type="custom" default="" label="" description="" />
Now, create a new PHP and place it inside your newly created "elements" folder and call it "custom.php". Then add the following code:
<?php
defined('_JEXEC') or die('Restricted access');
class JFormFieldCustom extends JFormField {
protected $type = 'Custom';
protected function getInput() {
return '<img src="' . JUri::root() . 'templates/your_template/images/image.jpg" alt="" />';
}
}
?>
Note that I haven't tested this so let me know if it works or not

can I use joomla's onAfterRender for a module rather than a plugin?

I want to insert some code to Joomla when any page is loaded.
For this I created a module that inserts code.
I am trying to use
<?php
// $Id: helper.php
defined('_JEXEC') or die;
jimport( 'joomla.plugin.plugin' );
jimport( 'joomla.environment.response' );
class modInsertCode
{
function onAfterRender($params)
{
$code = 'some code';
$documentbody = JResponse::getBody();
$documentbody = str_replace ("</body>", $code." </body>", $documentbody);
JResponse::setBody($documentbody);
return true;
}
}
?>
but JResponse::getBody(); returns an empty string. Any ideas, solutions of fixes to this code?
Thank you,
You have to do it using a plugin, you won't be able to do it using a module because the HTML response has not been generated by the time the code of the module gets executed.
I hope it helped!
I know this is a bit old but for future reference this can be done with jQuery:
$doc = JFactory::getDocument();
$js = 'jQuery(document).ready( function() {
jQuery("#module'.$module->id.'").appendTo(document.body);
})';
$doc->addScriptDeclaration($js);
This is assuming that you have wrapped the content in you module in something like the following, including the module id to support multiple instances of the module.
<div id="module<?php echo $module->id; ?>"> Your content </div>

Resources