Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Loop over variables from within a template

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
homerun31
Smarty n00b


Joined: 14 Sep 2018
Posts: 1

PostPosted: Fri Sep 14, 2018 4:17 pm    Post subject: Loop over variables from within a template Reply with quote

Is there a way to loop over each variable that's available in a template, without knowing which variables were passed to the template?

I've tried $smarty->getTemplateVars() from within the template but nothing gets outputted.

To give some context, I was given a job where I only have access to the template files, and not really able to edit the backend code easily. I also don't have any access to the server that the code lives on, so updates to the template files can sometimes take a few days to be set live. If there is a major issue with one of the templates, its possible that a decent amount of time can go by before a fix is made. So I was thinking of having each template output all of the available variables in a big json object, and then building a frontend on a server that I have more control over and then using curl or ajax calls to get the data I need. Any ideas on how to accomplish this?
Back to top
View user's profile Send private message
bsmither
Smarty Pro


Joined: 20 Dec 2011
Posts: 195
Location: West Coast

PostPosted: Fri Sep 14, 2018 6:11 pm    Post subject: Reply with quote

I have no experience at solving your specific approach.

My solution is to add {debug} to the end of the template I want to troubleshoot. See:
https://www.smarty.net/docs/en/language.function.debug.tpl

Once you can convince the server admins to add this to the instantiation of the Smarty class:
Code:
$smarty->debugging_ctrl = ($_SERVER['SERVER_NAME'] == 'localhost') ? 'URL' : 'NONE';
where $_SERVER['SERVER_NAME'] == 'localhost' can be any kind of test (such as IP address, or a passcode in the querystring, etc), then you can get a debug console on demand. See:
https://www.smarty.net/docs/en/chapter.debugging.console.tpl
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1399

PostPosted: Fri Sep 14, 2018 9:26 pm    Post subject: Re: Loop over variables from within a template Reply with quote

homerun31 wrote:
To give some context, I was given a job where I only have access to the template files, and not really able to edit the backend code easily. I also don't have any access to the server that the code lives on, so updates to the template files can sometimes take a few days to be set live. If there is a major issue with one of the templates, its possible that a decent amount of time can go by before a fix is made. So I was thinking of having each template output all of the available variables in a big json object, and then building a frontend on a server that I have more control over and then using curl or ajax calls to get the data I need. Any ideas on how to accomplish this?

You can use {debug} tag to see assigned variables, butů
Normally, this is accomplished by setting a development suite that you have full access to.
Smarty is not a self-contained application, you can't realistically separate it from the rest of the project.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP