Is it possible to construct an array in Smarty, for example I have tried
{def $totalitems[0]=3}
But that doesn't seem to work. Is it possible in Smarty?
Thanks.
I'm not sure why you would want to do this. The idea behind a template system is that you are separating the logic from the display. You need to build the array in PHP and then pass it into your smarty template using php like this:
$totalitems[0]=3;
$smarty->assign("totalitems",$totalitems);
Then you can access totalitems from within your template in the normal fashion.
In Smarty3 Beta you can do the following:
Examples:
{$foo['bar']=1}
{$foo['bar']['blar']=1}
Just look at the README: http://smarty-php.googlecode.com/svn/branches/Smarty3Dev/distribution/README
I am not sure if you can do it in Smarty2. I have tried a few things on my version of Smarty2, but it doesn't work. You might need to upgrade to Smarty3.
However, I would recommend against doing logic operations in the template, if it can be helped.
Related
I'm trying to create a messages.pot file for my application using the workflow described in the docs. Unfortunately, I can't get this to work. I can render the cached version of my templates, but when running xgettext, no strings are recognized.
After inspecting a cached template, I see calls being made to
echo $this->env->getExtension('translator')->getTranslator()->trans("Yadda", array(), "messages");
I guess xgettext only looks for calls to gettext(), dcgettext(), etc. Am I missing something here? How to fix this?
I'm using Silex 2.0.3-dev, twig 1.24.1, twig-bridge 3.0.7.
i had the same problem... i found no way to get it by the translator component, so i wrote and node js script to parse the twig files...
here is the pastebin link... if u like to update the file or something please contact me probably we can put it on github...
http://pastebin.com/WSDsABfz
I've seen several scripts during my time as a web programmer (I'm still new though) which had PHP included in an uncommon way.
The PHP was not added the usual way like <div><?php echo $foo; ?></div>, but this way: <div>{FOO}</div> or this <div>{$foo}</div>.
I am wondering how I could achieve such a thing? I really want to learn this thing.
Can someone direct me to the correct sources to learn this?
Thanks.
It has to be a template engine? You inject values to it from a PHP script. Smarty is a basic example.
For example, from your PHP page, you write :
include('Smarty.class.php');
$smarty = new Smarty;
$smarty->assign('foo', 'Bar Baz');
$smarty->display('index.tpl');
And, your template page would look like
<div>{foo}</div>
with .tpl extenstion.
When the program is run, the template variable {foo} will be replaced by its assigned value "Bar Baz"
The Developer must have been using a Template Engine Such as Smarty
You can even define arrays:
{assign var=foo value=[1,2,3]}
Objects
{$foo->bar}
The following would help you get started
http://www.9lessons.info/2011/09/smarty-template-engine-using-php.html
http://www.dreamincode.net/forums/topic/255552-an-introduction-to-smarty-php-template-engine/
http://www.moskalyuk.com/blog/smarty-faq
http://www.youtube.com/watch?v=5xLfvY8upsQ (Video)
I can not seem to find any documents stating if anonymous functions in templates are a good idea when templating HTML with PHP. I have the following code for example:
<html><body>
<?
$listMethod = function($items)
{
?>
<ul>
<?foreach ($items as $item):?>
<li><?=$item?></li>
<?endforeach;?>
</ul>
<?
};
?>
<?=$listMethod(array('1','2','3'))?>
<p> AND </p>
<?=$listMethod(array('a','b','c'))?>
</body></html>
Is this a good or bad way to create templates in PHP?
Bad bad way , it will be realy hard to debug them, edit, find, ... . You could include a template_functions.php file at the top of you're template and store all template related functions/helpers there .
Edit
Allso do not use short tags if you're into conding standards , most hosting companyes will allow them ( short tags ) but a few whont so you'll have problems .
Look at views as something that's just for output, nothing else.
Everything except echo, if and loops should concern you if in there. (Especially function definitions)
Is this a good or bad way to create templates in PHP?
That depends on whether or not you're intending to re-use that function in more than just one view. If you do, you might want to give the function a name, and put it somewhere so that multiple views can use it. If you don't want to use that function ever again, an anonymous function would do. Then again, I find anonymous functions to be less expressive than normal functions, so I don't think I'd use them for this precise purpose.
It's personal preference, though, and there's no consensus on this being good or bad.
Why scratch your left year with your right hand, as long as you have a left hand to do it? Best way is to define this as a classic function somewhere (myLeftyHelpers.php or whatever file). Although you made me wonder, I really do belive that the role of anonymus functions(if they work in PHP, never tried) are meant to act as function pointers, meaning that this way you can pass a function as a parameters to another function, that's the way I see them used. If someone thinks I am worng please correct me.
I am a PHP dev trying to start using HAML, using this implementation:
http://phphaml.sourceforge.net/
HAML looks awesome, but I don't understand if/how it supports partials (or includes, as they are called in the PHP world).
I would like to have a master template HAML file that then goes and loads up a bunch of partials for all the little pieces. (Then I can reuse those pieces in other templates too.)
In PHP or Ruby this would be really easy, is there any way to do this with HAML? thanks!
dylan
You could create a global render_haml_partial method by analogy with phpHaml's existing display_haml method that might look something like:
function render_haml_partial($sFilename, $aVariables = array(), $sTmp = true, $bGPSSC = false)
{
$sPath = realpath($sFilename);
$haml = new HamlParser(dirname($sPath), $sTmp);
$haml->append($GLOBALS);
if ($bGPSSC)
{
$haml->append($_GET);
$haml->append($_POST);
$haml->append($_SESSION);
$haml->append($_SERVER);
$haml->append($_COOKIE);
}
$haml->append($aVariables);
return $haml->fetch($sFilename);
}
This method could be placed in phpHaml's HamlParser.class.php file so it is available to all your templates.
The only difference between this and display_haml is that it invokes fetch instead of display at the end and returns the result so you can then insert it in-place into the invoking template.
You would then use it in your PHP/HAML templates as follows:
= render_haml_template("path to partial")
This would then be very similar to the Rails/HAML syntax:
= render :partial => 'path to partial'
Note that using display_haml directly does not have quite the same effect since it renders the template directly to the output instead of returning the result to the caller. Thus you could do the following:
- display_haml("path to partial")
But this doesn't capture the result of the render.
I'm guessing that somebody who cares enough about phpHaml might add such a render_haml_partial or something similar eventually - I might suggest it to the author some time.
Quite an old question, but I've updated the source code of phpHaml to reflect this new functionality!
Check out the commit #github
https://github.com/endorama/phphaml/commit/8d95d5ebff06275db8b14438e566c6e41ec91b7f
I am using Jumi to include a number of PHP scripts on Joomla! articles and it works great. The problem I am having is with passing variables (in the form of $_GET parameters) to a PHP script.
Lets say I have a script "index.php" and I wish to pass the $_GET[] parameter "var" with the value of "10". This would normally be accomplished by pointing to: index.php?var=10. How do "emulate" this functionality with Jumi? I was hoping it would be as simple as:
{jumi [directory/index.php] [var=10]}
The above syntax however is not correct.
Any input would be appreciated.
-- Nicholas
After some trial and error and guidance from the official Joomla! forums I did solve my problem. Rather than passing a true $_GET[] parameter you can pass a $jumi array and reference that.
I wanted to avoid having to rewrite much of my script so what I did was the following.
1) Make the Jumi call like this:
{jumi [directory/index.php] [value]}
2) In index.php:
if(isset($jumi[0]))
{
$_GET['PARAM_YOU_WANT_SET'] = $jumi[0];
}
This is a very simple example of a quick and easy way to emulate passing a $_GET[] parameter to a script using Jumi. This approach saved me a great deal of time because I didn't have to rewrite my controller.
-- Nicholas
This is an old thread I know but there is something that some people might want to know.
If you are wanting to use Jumi with extra parameters in a Module then Nicholas' tip won't work but there is a way to do it.
There is a "Code written" section of the module and a "Source of code" section.
Put the url/path to the file in the "Source of code" section and then define your variables in the "Code written" section...it will pass the variable to the source file before executing so it will do what is desired.