DEV Community
Posted on Nov 25, 2021 • Updated on Sep 14, 2022
Php Smarty template - assigning complex content to variables
In some cases Smarty doesn't allow you to assign values to variables just like that. For example in foreach loops. Then the following solutions can work. 1, Assign a simple text with variables:
explanation: Smarty's smart enough to replace value of variable within quotes.
2, Assign special characters or longer text:
explanation: the capture tag allows you to add text without quotes so it's readable. Variables have to be in {}.
3, Do ternary operation and assign value:
explanation: need to be aware of the brackets, but works fine. Normal brackets contain the condition.
Ternary operator concatenate variables with string (if the parentname is set, concatenate variables to a new string)
Top comments (0)
Templates let you quickly answer FAQs or store snippets for re-use.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink .
Hide child comments as well
For further actions, you may consider blocking this person and/or reporting abuse
Why Python Is Easier (Loops edition)
Chase - May 13
Optimizing the Performance of Django SearchVector with GIN Indexing
AJAY SHRESTHA - May 13
CNC Machining Service: Precision Engineering for Custom Parts Production
crispychip - May 13
PHP Primer: A Beginner's Guide
Rinesa - May 12
We're a place where coders share, stay up-to-date and grow their careers.
Smilyan Pavlov
Designer & web strategy consultant in sheffield, uk, smarty – how to assign a variable into a variable in the template.
Recently I needed to assign several variables into one smarty variable inside the template.
Here is the basic rules for the example:
- You have a text that says “Your registration expires in # days”;
- You want The text “in # days” to change to “today” if the # days is 0.
Here’s a way you can do this with smarty variables inside the template (whether you should do this in the template is not a discussion for this post).
The key here is that when you want to include a variable as part of the value of another variable you need to use value=” $in_days “ . Notice the ` before and after the variable name inside the double quotes. You can use this method to include variables inside value statements anywhere in smarty templates and make modifications of those included variables.
One thought on “ Smarty – How to assign a variable into a variable in the template ”
That helped a lot. Thanks!
Leave a Reply Cancel reply
Your email address will not be published. Required fields are marked *
- 07843 785658
- [email protected]
- Quiet Leaf Designs Ltd.
- 3 Arden House, DE45 1GT
- Documentation
- Mailing Lists
in English German Spanish French Italian Japanese Portuguese Russian General Development
About Smarty
Smarty icon.
You may use the Smarty logo according to the trademark notice .
Sites Using Smarty
Advertisement.
assign() — assign variables/objects to the templates
Description
You can explicitly pass name/value pairs, or associative arrays containing the name/value pairs.
If you pass the optional third nocache parameter of TRUE , the variable is assigned as nocache variable. See Cacheability of Variables for details.
When you assign/register objects to templates, be sure that all properties and methods accessed from the template are for presentation purposes only. It is very easy to inject application logic through objects, and this leads to poor designs that are difficult to manage. See the Best Practices section of the Smarty website.
Example 14.6. assign()
These are accessed in the template with
To access more complex array assignments see {foreach} and {section}
See also assignByRef() , getTemplateVars() , clearAssign() , append() and {assign}
Sponsors [info]
Smarty Copyright © 2002 – 2024 New Digital Group, Inc. All rights reserved.
This page generated in 0.01299 secs with TinyMVC and Smarty 3.
- Variable scopes
- From config files
- Combining Modifiers
- Config Files
- Debugging Console
Variables assigned from PHP
Variables assigned from PHP are referenced by preceding them with a dollar ( $ ) sign.
index.tpl source:
This above would output:
Associative arrays
You can also reference associative array variables by specifying the key after a dot "." symbol.
this will output:
Array indexes
You can reference arrays by their index, much like native PHP syntax.
This will output:
Properties of objects assigned from PHP can be referenced by specifying the property name after the -> symbol.
IMAGES
VIDEO
COMMENTS
{assign} is used for assigning template variables during the execution of a template. Note Assignment of variables in-template is essentially placing application logic into the presentation that may be better handled in PHP.
Assigning variables. Templates start to become really useful once you know how to use variables. Basic assigning. Let's revisit the example from the basics section.The following script assigns a value to the 'companyName' variable and renders the template:
Although Smarty can handle some very complex expressions and syntax, it is a good rule of thumb to keep the template syntax minimal and focused on presentation. If you find your template syntax getting too complex, it may be a good idea to move the bits that do not deal explicitly with presentation to PHP by way of plugins or modifiers.
explanation: Smarty's smart enough to replace value of variable within quotes. 2, Assign special characters or longer text:
Smarty assign value to an array variable index. 1. Performing in array like operation in smarty templates. 1. How to get the values from this array in Smarty template file? 2. how to assign associative array value in a smarty 2.x template without php script. 1.
Although Smarty can handle some very complex expressions and syntax, it is a good rule of thumb to keep the template syntax minimal and focused on presentation. If you find your template syntax getting too complex, it may be a good idea to move the bits that do not deal explicitly with presentation to PHP by way of plugins or modifiers.
Attribute Name Required Description; var: The name of the variable being assigned: value: The value being assigned: scope (optional) The scope of the assigned variable: 'parent','root' or 'global'
Recently I needed to assign several variables into one smarty variable inside the template. Here is the basic rules for the example: You have a text that says "Your registration expires in # days"; You want The text "in # days" to change to "today" if the # days is 0. Here's a way you can do this with smarty variables inside the ...
void assign (string varname, mixed var, bool nocache); You can explicitly pass name/value pairs, or associative arrays containing the name/value pairs. If you pass the optional third nocache parameter of TRUE, the variable is assigned as nocache variable. See Cacheability of Variables for details.
In a Smarty template file, you can assign an array to a variable using the assign function. The first parameter is the variable name, and the second parameter is the array.
Variables assigned from PHP. Variables assigned from PHP are referenced by preceding them with a dollar ($) sign.Examples
0. add {debug} into your template, it should show you all the variables that are assigned, if you are lucky, the variable you want is already there. If it isn't you need to write a bit of php code to assign it to the template. Depends on where/how you want it, but the more "future proof" is to create an extension (with civix) and use a hook ...
Therefore you should deal with your variables in the php page then assign them to the template as in Lucifer Orichalcum's example. If the user Id value you a trying to retrieve comes from a form you have to deal with the form values in the .php file ($_POST ['id']) not in the .tpl Same rule for your sessions variables.
Constants {#smarty.constants} Smarty Class Variables {#api.variables} Smarty Class Methods {#api.functions} Caching Resources Advanced Features {#advanced.features} Extending Smarty With Plugins {#plugins}
For help clarifying this question so that it can be reopened, visit the help center . Closed 13 years ago. How can i define variable not in PHP but in .TPL? (smarty template file) php. smarty. edited Nov 12, 2010 at 14:55. Svisstack. 16.5k 6 68 101. asked Nov 12, 2010 at 14:48.