|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
jplevene Smarty n00b
Joined: 10 Jun 2016 Posts: 2
|
Posted: Fri Jun 10, 2016 8:36 pm Post subject: Dynamic arrays |
|
|
Sorry in advance as I am new to smarty and accessing it for use.
I am working a a project that has thousands of available array variables for the template engine to access, however they are not assigned until a variable in the array is requested. For instance the following:
Code: | {{customer.name}}
{{customer.company}}
{{list.field1}}
{{list.field2}} |
Our code populates the customer array the first time it is called, the same for the list array (the list array is multidimensional which leads me to my next question).
Some arrays can also be multidimensional (like getting MySQL by $row->fetch_assoc*() in a PHP foreach loop). I assume the Smarty code would be:
Code: | {foreach from=$list}
<li>{{list.field1}} is {{list.field2}}</li>
{/foreach} |
My question is how do I dynamically assign the two different types of arrays (associative and multidimensional). Can I get Smarty to call my own function to get array values that are not yet set, etc. Would I use $smarty->registerDefaultPluginHandler('my_plugin_handler'); and how (especially for multidemensional arrays)?
Lastly, I know it seems a silly question, I assume I can put Smarty code inside an HTML comment (like <!-- {foreach from=$list) -->). The reason is that some HTML editors fix code they do not recognise in incorrect places, like surrounding a <tr>...</tr>.
Thanks in advance
J |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sat Jun 11, 2016 9:16 am Post subject: Re: Dynamic arrays |
|
|
jplevene wrote: | Sorry in advance as I am new to smarty and accessing it for use.
I am working a a project that has thousands of available array variables for the template engine to access, however they are not assigned until a variable in the array is requested. For instance the following:
Code: | {{customer.name}}
{{customer.company}}
{{list.field1}}
{{list.field2}} |
Our code populates the customer array the first time it is called, | Wat?…
Like, magically POP! and populate random variable as it is called?
Quote: | the same for the list array (the list array is multidimensional which leads me to my next question).
Some arrays can also be multidimensional (like getting MySQL by $row->fetch_assoc*() in a PHP foreach loop). | Stop doing that ffs! Use fetchAll() already.
Quote: | I assume the Smarty code would be: |
Code: | {foreach $list as $item}
<li>{{$item@key}} is {{$item}}</li>
{/foreach} |
Quote: | My question is how do I dynamically assign | You don't.
When you call up your template, all variables it need should be assigned already.
Quote: | Lastly, I know it seems a silly question, I assume I can put Smarty code inside an HTML comment (like <!-- {foreach from=$list) -->). | Smarty knows nothing about HTML, or any other markup, it just works with its own codes, that's all.
Quote: | The reason is that some HTML editors fix code they do not recognise in incorrect places, like surrounding a <tr>...</tr>. | And just why do you use HTML editor to edit a template?
Especially such a stupid one? How can you be sure that it is the least damage it does to your template? |
|
Back to top |
|
jplevene Smarty n00b
Joined: 10 Jun 2016 Posts: 2
|
Posted: Sat Jun 11, 2016 6:39 pm Post subject: Re: Dynamic arrays |
|
|
AnrDaemon wrote: | jplevene wrote: | Sorry in advance as I am new to smarty and accessing it for use.
I am working a a project that has thousands of available array variables for the template engine to access, however they are not assigned until a variable in the array is requested. For instance the following:
Code: | {{customer.name}}
{{customer.company}}
{{list.field1}}
{{list.field2}} |
Our code populates the customer array the first time it is called, | Wat?…
Like, magically POP! and populate random variable as it is called?
Quote: | the same for the list array (the list array is multidimensional which leads me to my next question).
Some arrays can also be multidimensional (like getting MySQL by $row->fetch_assoc*() in a PHP foreach loop). | Stop doing that ffs! Use fetchAll() already.
Quote: | I assume the Smarty code would be: |
Code: | {foreach $list as $item}
<li>{{$item@key}} is {{$item}}</li>
{/foreach} |
Quote: | My question is how do I dynamically assign | You don't.
When you call up your template, all variables it need should be assigned already.
Quote: | Lastly, I know it seems a silly question, I assume I can put Smarty code inside an HTML comment (like <!-- {foreach from=$list) -->). | Smarty knows nothing about HTML, or any other markup, it just works with its own codes, that's all.
Quote: | The reason is that some HTML editors fix code they do not recognise in incorrect places, like surrounding a <tr>...</tr>. | And just why do you use HTML editor to edit a template?
Especially such a stupid one? How can you be sure that it is the least damage it does to your template? |
Thanks for the obnoxious response and maybe you should read what I said rather than pretend to be a pompous know it all.
I said that I wanted to set the array on first call, not magically create it.
If the Smarty community is all like you then I will just recommend to our teams to stay away from it. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sun Jun 12, 2016 11:17 am Post subject: |
|
|
You don't need to quote entire post just to insert your snarky comment.
Programming language is not a magic, nothing comes out of thin air.
If you want variable in a template, you should assign it to the template.
Template needs to know only enough to render what is assigned to it. It should not care about where you get your variables, it's not its job to do business logic. |
|
Back to top |
|
|
|
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
|