How can I load my mustache template with PHP? - mustache.php

index.php:
<!-- language: lang-php -->
require('v/Mustache/Autoloader.php');
Mustache_Autoloader::register();
$m = new Mustache_Engine(array(
'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__) . '/v'),
));
// loads template from `v/sign_in.mustache` and renders it.
$m->render('sign_in', array('planet' => 'world'));
?>
v/sign_in.mustache:
<h1>Where in the {{planet}} is Carmen San Diego?</h1>
When I load index.php, I see a big blank screen with no errors.

echo $m->render('sign_in', array('planet' => 'world'));

Related

Manually control <head> markup in Joomla

Is there a way to manually configure the contents of the <head> section of the site in Joomla 3.1? I want to use the templating system for the entire markup of the page, including everything between <html></html>.
I just read this: http://forum.joomla.org/viewtopic.php?f=466&t=230787 and I am astonished at the response. Surely this is template/data separation 101. Has this been fixed in the latest Joomla release?
If you are planning for a template development and you need all your template data get separated from Joomla libraries or core file (the head section).
Normally the head section include will works like
<jdoc:include type="head" />
it loads the content from libraries libraries\joomla\document\html\renderer\head.php
If you want to override the content of head you can make a module for your task.
Just create a module and include that module instead of this head make sure that have all required codes added to work $document Class otherwise it miss a lot off features of Joomla regarding document class
As explained by the answer from Jobin, normally, you would include the head data by using the <jdoc:include type="head" /> tag, but if you want more control over this, you can use the JDocument.
Example code in your template's PHP:
$doc = JFactory::getDocument();
$my_head_data = $doc->getHeadData();
This will give you an array of the data that JDocument would normally print, so that you can completely choose what to print and how.
To make jQuery load from CDN and get it on top of the script list, I made a little patch just after the $doc = JFactory::getDocument(); that manipulates the header array directly inside the $this object as follows:
$my_jquery = "//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js";
$my_jquery_ui = "//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js";
$my_jquery_cx = $this->baseurl."/media/jui/js/jquery-noconflict.js ";
foreach($this->_scripts as $k=>$v) {
// put own jquery.conflict && jquery-ui && jquery on top of list
if( strpos($k,'jquery.min.js')) {
unset($this->_scripts[$k]);
$r = array( $my_jquery_cx => $v);
$this->_scripts = $r + $this->_scripts;
$r = array( $my_jquery_ui => $v);
$this->_scripts = $r + $this->_scripts;
$r = array( $my_jquery => $v);
$this->_scripts = $r + $this->_scripts;
}
else if( strpos($k,'jquery.ui.min.js')) {
unset($this->_scripts[$k]);
}
else if( strpos($k,'jquery-noconflict.js')) {
unset($this->_scripts[$k]);
}
}
Replace $my_jquery_xxx with editable config parameters in your templateDetails.xml file

How to call only one item from array with $this->config->item CODEIGNITER?

I have a default config file in my config directory (default.php). I've loaded it already with autoload file:
$autoload['config'] = array('default');
there is my default.php:
<?php
//$config['secretUserHashKey'] = 'g81h6JH18kASPkgAW16jS132sa186h1';
//$config['secretPhotoNameHashKey'] = 'ghOH3Hs841s98sssp1AHDWPfMHAjd';
// Pages
$config['pagesUrl'] = array(
'home' => 'homepage',
'login' => 'loginsite'
);
and how I should use it on my view?
<li>Home <span class="divider">/</span></li>
with this example I can get value if mine string is'nt array:
$config['home'] = 'homepage';
so what should I do to grab item from my array?
Try this:
<?php echo $this->config->item('home', 'pagesUrl');?>
I prefer this way:
$api_config = ci()->config->item('api');
if (isset($api_config['api_server']))
...
or in your case:
$pagesUrl_config = ci()->config->item('pagesUrl');
if (isset(pagesUrl['home']))
...
or in the view:
<?= #base_url(#ci()->config->item('pagesUrl')['home'])?>
this should do the trick
$this->config->config['pagesUrl']['home']
$this->config->item('item_name');
https://www.codeigniter.com/userguide3/libraries/config.html

CakePHP 2.0 css and scripts in element not being added to css/script blocks

I have just started a project with cakePHP 2.0. I am using an element to render a sidebar menu. I would like to specify the css and JS files for the menu in the menu element and not add them in my head tag (in case I'd like to conditionally render different sidebars).
For some reason my scripts and CSS are not being output.. any ideas why?
Layouts/Default.ctp
<head>
<?php
echo $this->fetch('css');
echo $this->fetch('script');
?>
</head>
....
<div id="leftcolumn">
<?php echo $this->element('sidebar/menu'); ?>
</div>
Elements/sidebar/menu.ctp
$this->Html->script('menu', array('inline' => false));
$this->Html->css('menu', null, array('inline' => false));
<div class="sidebarmenu"><ul><li>Menu Item</li></ul></div>
CSS and javascript are in webroot/css/menu.css and webroot/js/menu.js respectively.
If I put the Html->script and Html->css declarations in a view file or home.ctp or default.ctp they get added to the css and script blocks and are output just fine. When they are declared in the element file menu.ctp they don't work. Is there something I'm missing?
// css
$this->start('css');
echo $this->Html->css('additionalstyle');
echo $this->Html->css('anotherstyle');
echo $this->end()
// javascript
$this->start('script')
echo $this->Html->script('jquery');
$this->end()
Add this code in your view.ctp
http://book.cakephp.org/2.0/en/views.html
echo $this->Html->script('menu', array('inline' => false));
echo $this->Html->css('menu', null, array('inline' => false));
I think I got the problems. Main Problem is the order of css and js calling.
echo $this->fetch('css');
echo $this->fetch('script');
This two lines are echoing within head i.e. before calling you element. So there are two possible solutions for this issue:
cut above fetch() from head and append it before end of body tag so that it load scripts and css after all DOM available. i.e.
...
...
echo $this->fetch('css');
echo $this->fetch('script');
make "inline" => true i.e.
echo $this->Html->script('myel', array('inline' => true));
echo $this->Html->css('mycss', null, array('inline' => true));
I think this will solve your problems....

Cakephp with Ajax, this.element.setAttribute is not a function

I'm trying to make a stackoverflow like tags system.
I followed this tutorial (in French): http://www.formation-cakephp.com/34/autocomplete-en-ajax which uses Prototype and Scriptaculous. Of course, I adapted it to my project
I get the following error:
this.element.setAttribute is not a function : controls.js Line 86
which corresponds to
this.element.setAttribute('autocomplete','off');
in the control.js file
I'm really new to Ajax, so I don't have a clue on what I'm doing (wrong)...
If you need some code from any file, let me know!
view.ctp:
<div class="input">
<label>Tags :</label>
<?php e($ajax->autoComplete(
'Tag.tag',
'/tags/autocomplete',
array(
'minChars' => 3,
'indicator' => 'ajaxloader'
)
)); ?>
<div id="ajaxloader" style="display:none;">
Chargement...
</div>
Controller:
function autocomplete()
{
$recherche = utf8_decode($this->data['Tag']['tag']);
$tags = $this->Tag->find(
'all',
array(
'fields' => 'DISTINCT tag',
'conditions' => "tag LIKE '$recherche%'",
'order' => 'tag',
'limit' => 10
)
);
$this->set(compact('tag', 'recherche'));
}
jQuery, scriptaculous, & prototype don't play well together but you can resolve this issue by putting jQuery in no-conflict mode.
var $j = jQuery.noConflict();
// $j is now an alias to the jQuery function; creating the new alias is optional.
Now instead of using the $ to for jQuery use $j so for example:
$j(document).ready(function() {
$j( "div" ).hide();
});
For more information on avoiding jQuery conflicts refer to the following: https://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/
It appears that scriptaculous doesn't play well with j-query. When I removed the j-query link I stopped getting an error. This definitely isn't an ideal solution, but I thought I'd share my discovery.

Cakephp 1.3, Weird behavior on firefox when using $this->Html->link

Greetings,
I am getting a very weird and unpredictable result in firefox when using the following syntax:
$this->Html->link($this->Html->div('p-cpt',$project['Project']['name']) . $this->Html->div('p-img',$this->Html->image('/img/projects/'.$project['Project']['slug'].'/project.thumb.jpg', array('alt'=>$project['Project']['name'],'width'=>100,'height'=>380))),array('controller' => 'projects', 'action' => 'view', $project['Project']['slug']),array('title' => $project['Project']['name'], 'escape' => false),false);
OK I know it is big but bear with me.
The point is to get the following output:
<a href="x" title="x">
<div class="p-ctp">Name</div>
<div class="p-img"><img src="z width="y" height="a" alt="d" /></div>
</a>
I'm not sure if this validates correctly both on cakephp and html but it works everywhere else apart from firefox.
You can actually see the result here: http://www.gnomonconstructions.com/projects/browser
To reproduce the result use the form with different categories and press search. At some point it will happen!!
Although most of the time it renders the way it should, sometimes it produces an invalid output like that:
<div class="p-cpt">
name
</div>
<div class="p-img">
<img src="x" width="x" height="x" alt="x" />
</div>
Looks like it repeats the link inside each element.
To be honest the only reason I used this syntax was because cakephp encourages it.
Any help will be much appreciated :)
I am guessing that the name of some projects is null. According to the documentation, if you pass null as the second argument to the div() method, it will not generate the closing tag (and the resulting markup will be invalid).
The example of invalid markup that you pasted above appear to have come from Firebug rather than Page Source. Use Page Source to view the actual markup sent to the browser. The anchor tag is not repeated.
I rewrote your code to better see what happens. Copy it into one of your views, change 'My Project' to null, and notice how it will affect the $name_div variable:
<div class="p-cpt">My Project</div> will turn into <div class="p-cpt">.
<?php
$project['Project']['name'] = 'My Project';
$project['Project']['slug'] = 'my-project';
$image = $this->Html->image(
'/img/projects/' . $project['Project']['slug'] . '/project.thumb.jpg',
array(
'alt' => $project['Project']['name'],
'width' => 100,
'height' => 380
)
);
$name_div = $this->Html->div('p-cpt', $project['Project']['name']);
$image_div = $this->Html->div('p-img', $image);
$link = $this->Html->link(
$name_div . $image_div,
array(
'controller' => 'projects',
'action' => 'view',
$project['Project']['slug']
),
array(
'title' => $project['Project']['name'],
'escape' => false
)
);
var_dump($image);
echo 'Notice what happens below if project name is null.';
var_dump($name_div);
var_dump($image_div);
var_dump($link);
echo $link;

Resources