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

Few same templates with dfferent variables inside template

 
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
Ikerepc
Smarty n00b


Joined: 22 Mar 2018
Posts: 4

PostPosted: Thu Mar 22, 2018 11:38 pm    Post subject: Few same templates with dfferent variables inside template Reply with quote

So... Title is pretty weird...

So today I started with smarty as i found it a quite easy to use and I need some kind of tempalte system.

I want do make template system aviable to users on some way.
Like, they use part of my website and they have ability to edit design for their part of website.
So I don't want any sensitive information on templates. Only basic - html code and php echo.

That's why I choose template system in the first place.

Problem is that I'm using record from database multiple times. I don't want it to be visible in templates.

So I'm using something like this:

default/index/index.html:
Code:
<div class="text">
    <div class="kruh">
        <a href="index.php">{$l_ucenje}</a>
    </div>
   
    <div class="categories">
        {$kategorije}
    </div>
   
    <form action="" method="post">
        <button name="addnew" value="1" class="category">
            <div class="title">{$l_newucenje}</div>
            <p class="desc">{$l_newucenje_des}</p>
        </button>
    </form>

    <h4>{$l_statistika}</h4>
        <div class="sep"></div>
        <div class="stats"></div>
</div> 


default/index/index_category.html:
Code:
<div class="category">
    <div class="modify">
        <form action ="" method="post">
        <button type="submit" name="edit" value="{$ucid}">{$l_edit}</button>
        <button type="submit" name="delete" value="{$ucid}">x</button>
        </form>
    </div>
    <div class="title">
        <a href="poglavlja.php?ucid={$ucid}">{$title}</a>
    </div>
    <p class="desc">{$des}</p>
</div>


and index.php:
Code:
$s->assign("l_ucenje", $l['ucenje']);
$s->assign("l_newucenje_des", $l['newucenje_des']);
$s->assign("l_newucenje", $l['newucenje']);
$s->assign("l_statistika", $l['statistika']);

$sql = "SELECT ucid, title, des, ordered FROM ucenje ORDER BY ordered ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        display($row['ucid'], $row['title'], $row['des']);
    }
    if($iker->moderate_ucenje == 1) {
        echo '';
    }
}
else {
    $s->assign("nema_kategorija", $iker->notice($l['nema']));
}



function display($ucid, $title, $des) {
    global $s;
    global $iker;
    global $l;
    $s->assign("l_edit", $l['edit']);
    $s->assign("ucid", $ucid);
    $s->assign('title', $title);
    $s->assign('des', $iker->bb($des));
    $s->assign('kategorije', $s->fetch('default/index/index_category.html'));
}


What I want is to repeat this display() function with template for each db record.

I know It's really noobish peace of code hahha, I just started with smarty so I'm trying to figure out how it works before using it in some real code.

Or display few times template with different variables from database on any way. Just I want it to work with minimum of code in templates

Thanks in advance
Back to top
View user's profile Send private message Send e-mail
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Mar 23, 2018 1:01 am    Post subject: Reply with quote

First, opening your website templates for editing by foreigners is really, really, really bad idea and requires a friggin lot of though through and security precautions.
Better don't do that until you completely understand implications. But then again, you would not do that when you know what that means. The hell lies there.

Second, to the question of your templaye and database query, it requires a complete rewrite… Too much assignments, too much stuff to deal with. And it looks like you are using MySQLi, which does not make it easier at all.
Back to top
View user's profile Send private message
Ikerepc
Smarty n00b


Joined: 22 Mar 2018
Posts: 4

PostPosted: Fri Mar 23, 2018 1:11 am    Post subject: Reply with quote

So... Yeah, I know...

I'm using MySQLi. Also, I have around 4000 lines of php code and most of it (like 3,500) needs rewrite because of template system. But than it will be easier to keep it up.

I know It's not good idea to open templates to strangers. Noone will be allowed to access default templates, but some "important" members will be able to copy templates and make their own for some part of page they are using. Also, I will review it from time to time. That's why they are in map default. Other designs if exist will be in other maps. Also most probably I or some from my team will review that templates before letting it work.

I know it's a hell of work. I started working on it and I see. I haven't used any templates beore so I check out twig and this. This looks better to me right now... But it has a lot of work.

However, I'm ready to convert all my pages to templates (they need a review anyway...) but this is one of things which are really important o me....

So any help about that specific problem? I'm sure there is answer and it could be really simple but I was not able to find it...
Back to top
View user's profile Send private message Send e-mail
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Mar 23, 2018 1:33 am    Post subject: Reply with quote

Just pass the query result statement itself to template, and {foreach} over it.
Back to top
View user's profile Send private message
Ikerepc
Smarty n00b


Joined: 22 Mar 2018
Posts: 4

PostPosted: Fri Mar 23, 2018 1:36 am    Post subject: Reply with quote

Oh, well, yeah, that shoulddo the trick... Will try it tomorrow
Back to top
View user's profile Send private message Send e-mail
Ikerepc
Smarty n00b


Joined: 22 Mar 2018
Posts: 4

PostPosted: Fri Mar 23, 2018 10:46 am    Post subject: Reply with quote

Why is it so complicated???

So I did it but I need to make new if statement for each permission in templates?

That will make my templates bigger than original code and with more "php code" in it because I can't use basic php functions.

What if I want for some functions for thing from database to be aviable only to some users?

I need to have foreach for display of categories (or anything else) for each function and different php code for each user because I don't want all functions to be sent to template always. Some need to be sent just for some users

Like one usergroup can only edit categories, one can delete or approve it and one can't anything. And I have even more functions for other pages.

It can't be that complicated, softwares used it.
Back to top
View user's profile Send private message Send e-mail
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Mar 23, 2018 7:00 pm    Post subject: Reply with quote

Do permission checks in your PHP code, and only pass to template the data that already prepared for display.
Don't try to turn template into another language for your application.
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