|
- Share on Google
- Share on Facebook
- Share on Twitter
- View a Printable Version
- Subscribe to this thread
- Add Poll to this thread
- Send thread to a friend
- Linear Mode
- Threaded Mode
|
11-21-2012, 04:00 AM I was wondering what is the equivalent of the code above Smarty. Cause $this->smarty->assign("places", $list_places); does not create global variables! Thank you 11-23-2012, 08:05 PM If you want to see a way of integrating Smarty but keeping CI-style code (aka $this->load->view()), see my Github Repo: |
|
CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications.
- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand
- OverflowAI GenAI features for Teams
- OverflowAPI Train & fine-tune LLMs
- Labs The future of collective knowledge sharing
- About the company Visit the blog
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Get early access and see previews of new features.
Get smarty variable into JavaScript
I want to make a simple snippet. I would like to console.log a smarty variable. I tried :
And in my JavaScript
It give me a blank page and nothing in the console log. In log I get
Smarty Error: Syntax error in template "file:/usr/share/nginx/html/page.tpl" on line 367 "{/literal}{$smarty.lbrace}{literal}" $smarty.lbrace is not defined
I also tried
It output {$somevar}
console.log return blank
![smarty assign global variable executable's user avatar](https://www.gravatar.com/avatar/c34270469c0d7acf916568cd5561ab2d?s=64&d=identicon&r=PG&f=y&so-version=2)
- The second won't work because it's considering {$somevar} as a literal too, that's why you need to stop the literal and restart it. The first one not working is weird tho. Could you add the full hierarchy and if you have anything else in your file? – Alexandre Elshobokshy Commented Feb 20, 2019 at 13:03
- This code is in the same file. I'm using it on whmcs – executable Commented Feb 20, 2019 at 13:04
- 1 I fixed, it was a typo error, sorry about that ! But I still cannot log new_var – executable Commented Feb 20, 2019 at 13:09
- If you try to console.log("string test") does it show the result? – Alexandre Elshobokshy Commented Feb 20, 2019 at 13:09
- @Islam Yes I can output in console.log. It look like the new_var is empty but I don't know why – executable Commented Feb 20, 2019 at 13:10
First of all, declare your smarty variable :
And, use it in your .tpl file who will contains your JavaScript in < script> tag :
if you want to use a value from the PHP, you assign it to the smarty object, call the view, and display it with the same method.
![smarty assign global variable Fizik26's user avatar](https://lh3.googleusercontent.com/-XAzkrkEpit0/AAAAAAAAAAI/AAAAAAAAAEg/AWErc7qYyNI/photo.jpg?sz=64)
- He's not looking to assign a smarty object, he's looking to use a PHP assigned variable inside smarty. I'm not sure you really understood the question. – Alexandre Elshobokshy Commented Feb 20, 2019 at 15:14
Your Answer
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Sign up or log in
Post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .
Not the answer you're looking for? Browse other questions tagged php smarty or ask your own question .
- The Overflow Blog
- The framework helping devs build LLM apps
- How to bridge the gap between Web2 skills and Web3 workflows
- Featured on Meta
- Upcoming initiatives on Stack Overflow and across the Stack Exchange network...
- Announcing a change to the data-dump process
- What makes a homepage useful for logged-in users
Hot Network Questions
- Infinite output impedance in amplifier, instead of 0?
- Why are there two cables connected to this GFCI outlet?
- Where does the energy in a fundamental interaction come from?
- What make and model of aircraft is this?
- When can widening conversions cause problems?
- How do I know if a motion is 1 dimensional or 2 dimensional?
- What is the name of the floor pump part that depresses the valve stem?
- A story about a personal mode of teleportation, called "jaunting," possibly in Analog or Amazing Stories
- Can a web-service based on AGPL 3.0 Licensed software demand money for its services?
- How to receive large files guaranteeing authenticity, integrity and sending time
- The Zentralblatt asked me to review a worthless paper, what to do?
- Can your boss take vouchers from you, offered from suppliers?
- What goods yield the best profit for time-travel arbitrage?
- How to request for a package to be added to the Fedora repositories?
- What concretely does it mean to "do mathematics in a topos X"?
- Why not use computers to evaluate strength of players?
- What is the meaning of the "Super 8 - Interactive Teaser" under "EXTRAS" in Portal 2?
- I can't find a nice literal translation for "Stella caelis exstirpavit"
- What are good reasons for declining to referee a manuscript that hasn't been posted on arXiv?
- Is "Ἐλλάχ" the proper Greek transliteration of "Allah"?
- Would human-shaped sperm cells work?
- replacing a 15-amp breaker with a 20-amp breaker
- Why can THHN/THWN go in Schedule 40 PVC but NM cable (Romex) requires Schedule 80?
- Left crank arm misaligned on climb
![smarty assign global variable](https://stackoverflow.com/posts/54786977/ivc/da4d?prg=f252c387-3117-4ba8-a922-6ef7d40e9843)
![](//mypaperwriting.best/777/templates/cheerup1/res/banner1.gif)
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.
How to set a Smarty Variable. In case you want to set a smarty variable you will have first to assign it. You can do it through a module, controller or a TPL. From a module or a controller it will be available as soon as you declare it. To print or to debug it.
Assigning to template objects. When you use a template objects, as explained in rendering a template, you can assign data to the template objects directly instead of assigning it to Smarty. This way, you can use different sets of data for different templates.
You may use the Smarty logo according to the trademark notice.. For sponsorship, advertising, news or other inquiries, contact us at:
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'
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.
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.
Hi, I would like to know how I can add a variable in every smarty template. For example, we can use {$urls.base_url} because $urls is an array that has been assigned ...
Variable scopes. You have the choice to assign variables to the scope of the main Smarty object, data objects created with createData () , and template objects created with createTemplate (). These objects can be chained. A template sees all the variables of its own object and all variables assigned to the objects in its chain of parent objects.
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 ...
Unable to assign global scope variables #194. Closed machavity opened this issue Feb 23, 2016 · 3 comments ... which relies on an assigned global variable, was being assigned in another previous subtemplate. ... This assignment works fine in 3.1.17 but appears to no longer work in the current version of Smarty. SubTemplate 1 {assign var ...
Prestashop Tutorials. /. Prestashop 1.7: Smarty global variables. Image: People vector created by freepik - www.freepik.com. When developing your ecommerce site, you may need to view an item based on a condition or not. To do that, you need to edit the theme files using some Smarty language variables used by Prestashop.
Although Smarty provides direct access to PHP super globals for convenience, it should be used with caution. Directly accessing super globals mixes underlying application code structure with templates. A good practice is to assign specific needed values to template vars.
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.
Some Modules seems not to implement the right Smarty-Variable for that. For example Homeslider & advertising_block & Product-Highlight on startpage still use default-domainname for the images. Link to comment
I think the use of scope is not applicable here. Scope wants to have the variable and value be seen in the template hierarchy (that is, included sub-templates), not the logical hierarchy (that is, functions). So, we should accept that variables created or modified inside a Smarty function will not be known outside that function.
CodeIgniter Forums Archived Discussions Archived Development & Programming Smarty: Assigning global variable from controller
Then, you need to assign this php variable to a Smarty variable to show the value in the tpl file. To do this we use the method used by "@Ravinder Pal" but changing the value: ... Where define new global variable in Prestashop. 1. Initialize a constant variable in PS_CONFIGURATION. 0. Global JS variable in Prestashop 1.6. 1. SMARTY: Variable ...
1. First of all, declare your smarty variable : And, use it in your .tpl file who will contains your JavaScript in < script> tag : if you want to use a value from the PHP, you assign it to the smarty object, call the view, and display it with the same method.