Smarty value in value - smarty

How to run like this code
$smarty->assign('value','foo');
$smarty->assign('value1','some smarty code with {$value}');
In the template, if I use {$value}, there can't show value.

You can do this as below in PHP:
$smarty->assign('value','foo');
$smarty->assign('value1',"some smarty code with {$smarty->getTemplateVars('value')}");
Then in Smarty template file:
{$value}
{$value1}
Both variables are visible and working in Smarty template file.
However I've never used such functionality. I would rather do it this way:
$fooVariable = 'foo';
$smarty->assign('value',$fooVariable);
$smarty->assign('value1','some smarty code with {$fooVariable}');

Related

Get {{.Host}} inside a range loop - Caddy server browse template

So I can use {{.Host}} just fine in the template file, but once inside a {{range .Items}} loop it doesn't work since it's trying to get the .Host from the .Items (array?)thing..
I get this as an error
template: listing:41:46: executing "listing" at <.Host>: can't evaluate field Host in type browse.FileInfo
I've never used Go before, I've tried reading the text template documentation page but it's all rather confusing.
ooooh, nevermind guys, I knew it was a simple fix.
{{$.Host}}
Just add the $, then you'll be using the global context again, instead of the context inside of the range loop.
Source, thanks HUGO for the clear documentation.
{{range}} changes the pipeline (the dot, .) to the current Items. You can use {{$.Host}} which will refer to the "top-level" Host.
{{$.Host}}
golang template.

Sass function to add prefix before css class [duplicate]

I am trying to save a selector in SASS for easier referencing later, however I get a syntax error.
Here's what I'm trying to do:
$icon: [class*="icon"];
You need to convert it to a string if you want to use it as a variable:
$icon: '[class*="icon"]';
#{$icon} {
// stuff
}

codeigniter - use custom config value inside CI config file

I got custom.php (inside config folder) with code like below
$config['myurl'] = 'somesite.com';
I got config.php ( standard CI ), I want to set the base_url using the value from custom.php, like this
$config['base_url'] = $this->config->item('myurl');
Doing that I got the error
Using $this when not in object context in /Volumes/HD 2/work/vnl/app/config/config.php on line 18
Whats the right code for this purpose?
This is not going to work there. The $this that you would want to use there is a controller instance and that is not created yet when the standard application/config/config.php loads.
You can try to add a pre_system hook (that's loaded right before the config object) and include in some helper function that can be called and return the desired value in the application/config/config.php. The usual constants like APPPATH will be available then.
If you can make this config variable a constant that can work too (just put the define() inside application/config/constants.php). Since the config file is just a regular php source file, you can have conditionals here too if you must.
You can just work with the arrays.
In your custom config file
$config['myurl'] = 'myurl.com';
$config['base_url'] = &$config['myurl'];
This will change the default base_url once the custom config file is loaded, you can also just reset the base_url element in your custom config, however in my opinion it is not recommended to reset the base url in another config file as later in the project you could lose track of where it is set, why not set it (and maybe use conditions) in the main config file?

Variable autoescape in Smarty templates

I have recently found out that Smarty, differently from Django template engine, does not escape variables automatically and I need to put |escape next to most of the variables in my templates.
Following the docs, http://www.smarty.net/docsv2/en/variable.default.modifiers.tpl I need to set default modifiers, needn't I?
So, here's my code:
$smarty = new Smarty();
$smarty->default_modifiers = array('escape:"htmlall"');
... and still variables ARE NOT escaped until I add |escape next to them.
What am I doing wrong?
If you are on Smarty 3, try this:
$smarty = new Smarty();
$smarty->loadFilter(Smarty::FILTER_VARIABLE, "htmlentities");
Tadà!
Update: Smarty::FILTER_VARIABLE is undocumented as of 28/11/2014. Use $smarty->escape_html = true if you want to stick to offical docs.
It appears that this feature was removed from Smarty v3, and docs are outdated. See:
http://www.smarty.net/forums/viewtopic.php?p=62207
I'd recommend a workaround - which is template level. Either create a new style v3 function to take care of filtration, or, do a simple include.
Include method
Put this in a clean.tpl file:
{$text|escape:htmlall}
Then invoke as {include file=clean.tpl text=$myvariabletofilter}
Function method
The new functions in Smarty could also take care of that:
{function clean}
{$text|escape:htmlall}
{/function}
And invoke as {clean text=$myvariabletofilter}
As always, make sure that these things get trimmed right and don't insert unncessary spaces.

smarty template engine

I have an webpage written using php but now need to convert them to smarty template engine.
I am rookie to smarty template engine.I find it difficult of smarty.net document.Is there any book or site from where i can learn smarty.
I need a small snippet for insert function.
I think the smarty.net documentation is the very best point to start using Smarty Template Engine.
Most tutorials are based on the documentation I think.
For a very quick start you just have to require the Smarty base class:
// Require base class
require_once('Smarty.class.php');
// create new Smarty instance
$smarty = new Smarty();
// define Smarty directories
$smarty->template_dir = '/web/www.example.com/guestbook/templates/';
$smarty->compile_dir = '/web/www.example.com/guestbook/templates_c/';
$smarty->config_dir = '/web/www.example.com/guestbook/configs/';
$smarty->cache_dir = '/web/www.example.com/guestbook/cache/';
// define a template variable, which will be shown in your template
$smarty->assign('greeting', 'Hello World!');
// force your php script to show your template file
$smarty->display('template.html');
Thats everything you need in your PHP file.
In the default configuration of Smarty you can show your template variable greeting by using
My greeting message: {$greeting}
If you open your PHP File via a browser you will see: My greeting message: Hello World!
For further information you really should read the official Smarty documentation!

Resources