|
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 |
shatyro Smarty n00b
Joined: 23 Aug 2017 Posts: 2
|
Posted: Sun Aug 27, 2017 8:16 pm Post subject: Right way to fetch / submit data from controller to view? |
|
|
Hey there,
today I was working working on a navigation bar, that is connected with a right permission system. The controller gets the right permissions and sends it to the view (which is the smarty engine).
I sent all nescessary data in one multiple nested array and it was terrible and took me hours to figure out how exactly I can fetch everything so that it fits in HTML.
Here is my template file, that gets $navi as data
Code: |
{block name=navigation}
<nav class="navbar navbar-toggleable-md navbar-light bg-faded fixed-top">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="">
<img src="app/img/logo.png" height="20" class="d-inline-block" alt="">
</a>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav ml-auto">
{foreach key=mainNavi item=mainNaviItm from=$navi}
{if sizeof($mainNaviItm["sub"]) == 1}
{foreach key=subNavi item=subArr from=$mainNaviItm["sub"]}
<li class="nav-item">
<a class="nav-link" href="{$subArr["url"]}">
{if isset($mainNaviItm["icon"])}
<i class="fa fa-{$mainNaviItm["icon"]}" id=nav_{$mainNaviItm["icon"]} aria-hidden="true"></i>
{/if}
{$mainNavi}
</a>
</li>
{/foreach}
<br>
{else}
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="http://example.com" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{if isset($mainNaviItm["icon"])}
<i class="fa fa-{$mainNaviItm["icon"]}" id=nav_{$mainNaviItm["icon"]} aria-hidden="true"></i>
{/if} {$mainNavi}</a>
<div class="dropdown-menu" aria-labelledby="dropdown01">
{foreach key=subNavi item=subArr from=$mainNaviItm["sub"]}
<a class="dropdown-item" href="{$subArr["url"]}">{$subNavi}</a>
{/foreach}
</div>
</li>
{/if}
{/foreach}
<li class="nav-item">
<a class="nav-link" href="login/logout"><i class="fa fa-sign-out" aria-hidden="true"></i> Logout</a>
</li>
</ul>
</div>
</nav>
{/block}
|
And that is the nested data array $navi
Code: |
array(3) {
["Fahrtmonitor"]=>
array(1) {
["sub"]=>
array(1) {
["Fahrtmonitor"]=>
array(1) {
["url"]=>
string(12) "liveplan.php"
}
}
}
["Nachrichten"]=>
array(2) {
["sub"]=>
array(1) {
["Fahrer"]=>
array(1) {
["url"]=>
string(8) "messages"
}
}
["icon"]=>
string(10) "commenting"
}
["Admin"]=>
array(2) {
["sub"]=>
array(6) {
["Benutzerübersicht"]=>
array(1) {
["url"]=>
string(9) "users.php"
}
["Benutzer hinzufügen"]=>
array(1) {
["url"]=>
string(12) "add_user.php"
}
["Gruppe hinzufügen"]=>
array(1) {
["url"]=>
string(13) "add_group.php"
}
["Gruppe editieren"]=>
array(1) {
["url"]=>
string(14) "edit_group.php"
}
["Traffic anzeigen"]=>
array(1) {
["url"]=>
string(11) "traffic.php"
}
["Benutzer editieren"]=>
array(1) {
["url"]=>
string(13) "edit_user.php"
}
}
["icon"]=>
string(4) "lock"
}
}
|
Is it the right way to submit the data from a controller to a view with smarty or is there a better way ?
Thanks! |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Sun Aug 27, 2017 8:39 pm Post subject: |
|
|
I see nothing wrong with your approach.
Using the {$varname["key"]} syntax is fine, although I would say the {$varname.key} syntax is more prevalent.
What was your essential difficulty in planning your array? Determining what data to populate it with? It's hierarchical structure? The Smarty syntax to show it?
Last edited by bsmither on Mon Aug 28, 2017 9:30 pm; edited 1 time in total |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon Aug 28, 2017 4:54 pm Post subject: |
|
|
I have a different question.
Why "{if sizeof($mainNaviItm["sub"]) == 1}" ?
And as said, what was the difficulty? I see you've mentioned access restrictions, how did you solve them? |
|
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
|