Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Dynamic arrays

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
jplevene
Smarty n00b


Joined: 10 Jun 2016
Posts: 2

PostPosted: Fri Jun 10, 2016 8:36 pm    Post subject: Dynamic arrays Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Jun 11, 2016 9:16 am    Post subject: Re: Dynamic arrays Reply with quote

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
View user's profile Send private message
jplevene
Smarty n00b


Joined: 10 Jun 2016
Posts: 2

PostPosted: Sat Jun 11, 2016 6:39 pm    Post subject: Re: Dynamic arrays Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Jun 12, 2016 11:17 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    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