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

Is there a better way or a function that I could write?

 
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
Strider64
Smarty Rookie


Joined: 30 Sep 2017
Posts: 6

PostPosted: Sat Sep 30, 2017 3:37 pm    Post subject: Is there a better way or a function that I could write? Reply with quote

First, I been developing in PHP for 7 or 8 years and just now looking into using Smarty to able to write templates that I can use for the websites that I design and develop. That way when I do get clients I can just concentrate on all the design aspects of their website, for the backend development for the most part should be done and will not have to be touched. It would also be nice for someone at the company that could in an emergency try to fix the design portion of their website without having to worry about them messing up the backend portion of the website.

Right now I'm in the beginning stages of implementing Smarty and I have the following ->

Code:
        <div id="website" class="container">
            <div class="span6">
               
                    {* Create a CMS Display using Smarty *}
                    {foreach item=value from=$l_threads}
                        <article class="cms">
                            <h2>{$value.heading}</h2>
                            <h3 class="subheading">Created on {$value.date_added} by {$value.author}</h3>
                            <figure class="imageStyle">
                                <img src="{$value.image_path}" alt="{$value.heading}">
                                <figcaption>&nbsp;</figcaption>
                            </figure>
                                <p>{$value.content|escape}</p>
                        </article>
                    {/foreach}

           
            </div>
            <div class="span6">
                    {* Create a CMS Display using Smarty *}
                    {foreach item=value from=$r_threads}
                        <article class="cms">
                            <h2>{$value.heading}</h2>
                            <h3 class="subheading">Created on {$value.date_added} by {$value.author}</h3>
                            <figure class="imageStyle">
                                <img src="{$value.image_path}" alt="{$value.heading}">
                                <figcaption>&nbsp;</figcaption>
                            </figure>
                                <p>{$value.content|escape}</p>
                        </article>
                    {/foreach}
            </div>


The articles display Heading, Sub-Heading, Image and Paragraph to two separate spans (columns) and other than that they are identical. Is there a way to write a function to accomplish this? From what I read about plugins is to keep the business aspect of the website separated from the presentation portion of site. That makes sense to have the php at the sending end and using Smarty on the template side of it. It would be nice just to have minimum code on the template side that way practically anyone who knows HTML/CSS could figure it out after studying it for a few minutes. If someone could just point me to a good tutorial or the right direction would be greatly appreciated.

Thanks John
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Sep 30, 2017 6:27 pm    Post subject: Re: Is there a better way or a function that I could write? Reply with quote

Why do you use Smarty2 ?
Back to top
View user's profile Send private message
Strider64
Smarty Rookie


Joined: 30 Sep 2017
Posts: 6

PostPosted: Sat Sep 30, 2017 9:21 pm    Post subject: Reply with quote

I download Smarty3 and I pretty positive I'm using it. Unless I'm using Smarty2 Syntax/functions? Like I said I am new to Smarty.

Says 3.1.30 for the version {$smarty.version}
Back to top
View user's profile Send private message
bsmither
Smarty Elite


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

PostPosted: Sat Sep 30, 2017 9:39 pm    Post subject: Reply with quote

Your code shows two six-wide columns. Within each column, the code is iterating through the array completely each time.

Are you wanting one article per block? Or maybe half the articles in the first block, the other half in the second block?

If splitting by half, we can use for the first half:
Code:
{if $value@index gte $value@total / 2}{break}{/if}
This will cycle until the iteration is after the half-way point.

Then we start again, but for the second half:
Code:
{if $value@index lt $value@total / 2}{continue}{/if}
This will cycle through, bypassing all elements, until after the half-way point.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


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

PostPosted: Sat Sep 30, 2017 9:58 pm    Post subject: Reply with quote

Looking at your code again, I see that you have created two distinct lists of threads, "l_' and "r_" arrays.

Is it your wish to have the PHP code not try to pre-calculate how many elements go in each array? To simply assign the entire article threads resultset to one single array - thus requiring display logic to lay it out according to specific per-customer designs?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Oct 01, 2017 3:21 am    Post subject: Reply with quote

Strider64 wrote:
I download Smarty3 and I pretty positive I'm using it. Unless I'm using Smarty2 Syntax/functions? Like I said I am new to Smarty.

Says 3.1.30 for the version {$smarty.version}

Then why you're using Smarty2 syntax?
And answering your original question, you should not "optimize" your templates, unless proven that this is your weak point.
If you want to separate a common code block into a subtemplate (for easier maintenance, for example), use https://www.smarty.net/docs/en/language.function.include.tpl#idp35758448
You can pass random variables to the included template, making it behave close to a PHP function.
Back to top
View user's profile Send private message
Strider64
Smarty Rookie


Joined: 30 Sep 2017
Posts: 6

PostPosted: Sun Oct 01, 2017 12:17 pm    Post subject: Reply with quote

AnrDaemon wrote:
Strider64 wrote:
I download Smarty3 and I pretty positive I'm using it. Unless I'm using Smarty2 Syntax/functions? Like I said I am new to Smarty.

Says 3.1.30 for the version {$smarty.version}

Then why you're using Smarty2 syntax?
And answering your original question, you should not "optimize" your templates, unless proven that this is your weak point.
If you want to separate a common code block into a subtemplate (for easier maintenance, for example), use https://www.smarty.net/docs/en/language.function.include.tpl#idp35758448
You can pass random variables to the included template, making it behave close to a PHP function.


Ok looking over Smarty3 documentation I am assuming the following would be Smarty3?
Code:
                {* Create a CMS Display using Smarty *}
                {foreach $l_threads as $value}
                    <article class="cms">
                        <h2>{$value.heading}</h2>
                        <h3 class="subheading">Created on {$value.date_added} by {$value.author}</h3>
                        <figure class="imageStyle">
                            <img src="{$value.image_path}" alt="{$value.heading}">
                            <figcaption>&nbsp;</figcaption>
                        </figure>
                        <p>{$value.content|escape}</p>
                    </article>
                {/foreach}


Which is more in line with PHP syntax and makes a little more sense than Smarty2 syntax.
Back to top
View user's profile Send private message
Strider64
Smarty Rookie


Joined: 30 Sep 2017
Posts: 6

PostPosted: Sun Oct 01, 2017 12:31 pm    Post subject: Reply with quote

bsmither wrote:
Looking at your code again, I see that you have created two distinct lists of threads, "l_' and "r_" arrays.

Is it your wish to have the PHP code not try to pre-calculate how many elements go in each array? To simply assign the entire article threads resultset to one single array - thus requiring display logic to lay it out according to specific per-customer designs?


First thanks for the help.

Looking it over, I think when I convert over to Smarty Templating I will just let PHP do all the heavy lifting in the background and be smarter with my naming of variables.

The one thing right I know that I don't like about smarty is the include statements, but maybe looking over Smarty3 Documentation there are better ways writing code without have to resort to using `include`. Section on objects is probably where to look at. I bought a book on Smarty but quickly turned off by it, for one it's taking too long even to get into design aspects of Smarty (Already on Chapter 4) and second it's still using obsolete PHP code (mysql instead of mysqli or PDO). I can't stand reading or try run example code that is obsolete and it's a relatively new edition of the book. Evil or Very Mad Oh, well I just read the Smarty3 Documentation here I guess.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Oct 01, 2017 2:17 pm    Post subject: Reply with quote

I don't know what you don't like about {include}, but may be you find {extends} more entertaining.
See https://www.smarty.net/forums/viewtopic.php?t=25486 for usage example.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Oct 01, 2017 2:24 pm    Post subject: Reply with quote

Another word regarding include vs. extends: my personal choice is to use "extends" for blocks directly relevant to page content and include for decoration.
Plus a heavy use of custom rendering plugins and modifiers.
Back to top
View user's profile Send private message
Strider64
Smarty Rookie


Joined: 30 Sep 2017
Posts: 6

PostPosted: Tue Oct 03, 2017 4:41 pm    Post subject: Reply with quote

AnrDaemon wrote:
Another word regarding include vs. extends: my personal choice is to use "extends" for blocks directly relevant to page content and include for decoration.
Plus a heavy use of custom rendering plugins and modifiers.


Thanks by pointing out extends to me, I like extends for I think it will give people who might be "modifying" the design layout of the webpage(s) an easier time if they can concentrate on the job on hand instead of wondering (or be distracted) by those include statements. I like the fact that it's easy to have separate CSS files if you chose to do so. I also like the fact it will be easier to change the look of the website easier for you can change HTML and CSS on the fly. Templating kind of reminds me little bit like CSS Zen Garden http://www.csszengarden.com/ where one can learn css better, but with only the added feature of fooling with the HTML. Thanks Again.

I'm also starting to like the Documentation here for Smarty, it's starting to make sense to me.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Oct 03, 2017 6:36 pm    Post subject: Reply with quote

You shouldn't second-guess the theoretical future users of your application. It's not only a senseless, but a highly detrimental approach to your productivity.
{extends} is not "easier" or "harder" than {include}, it's flat out different.
Back to top
View user's profile Send private message
Strider64
Smarty Rookie


Joined: 30 Sep 2017
Posts: 6

PostPosted: Wed Oct 04, 2017 5:25 pm    Post subject: Reply with quote

AnrDaemon wrote:
You shouldn't second-guess the theoretical future users of your application. It's not only a senseless, but a highly detrimental approach to your productivity.
{extends} is not "easier" or "harder" than {include}, it's flat out different.


I understand that and I probably probably worded it wrong. Never was that good at English. Very Happy I understand that if a person doesn't have a little bit of know of what an if statement is, no matter what you do he/she is going to have a hard time. I just think using a template engine such as Smarty rids a person who is already knows HTML/CSS the trouble of weeding through PHP code. Most designers have a little bit of knowledge of Javascript, so seeing an if statement or a loop in Smarty shouldn't shock them too much.

However seeing something like the following might:

Code:
$submit = filter_input(INPUT_POST, 'submit', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
if (isset($submit) && $submit === 'submit') {
    $token = filter_input(INPUT_POST, 'token', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    if (!empty($token)) {
        if (hash_equals($_SESSION['token'], $token)) {
            /* The Following to get response back from Google recaptcah */
            $url = "https://www.google.com/recaptcha/api/siteverify";

            $remoteServer = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_SANITIZE_URL);
            $response = file_get_contents($url . "?secret=" . PRIVATE_KEY . "&response=" . \htmlspecialchars($_POST['g-recaptcha-response']) . "&remoteip=" . $remoteServer);
            $recaptcha_data = json_decode($response);
            /* The actual check of the recaptcha */
            if (isset($recaptcha_data->success) && $recaptcha_data->success === TRUE) {
                $data['name'] = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
                $data['email'] = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
                $data['phone'] = filter_input(INPUT_POST, 'phone', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
                $data['website'] = filter_input(INPUT_POST, 'website', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
                $data['reason'] = filter_input(INPUT_POST, 'reason', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
                $data['comments'] = filter_input(INPUT_POST, 'comments', FILTER_SANITIZE_FULL_SPECIAL_CHARS);

                $send = new Email($data);
            } else {
                $success = "You're not a human!"; // Not of a production server:
            }
        } else {
            // Log this as a warning and keep an eye on these attempts
        }
    }
}


I belong to PHP forums and I have seen a lot of threads that start off with "I'm not a PHP programmer and I just want to modify my HTML. However I don't know how!......"

I think templates get rid of that issue or at least I hope so.

As for {include} and {exclude} I have been finding out that both have a nice role in templating. {exclude} I been finding out serves as a way to design the whole html page which to me comes in handy from a designer perspective (at least I think so) and {include} makes repetitive changes easier for it's located in one area or include html that is used on multiple pages such as a navigational menu simpler. One change to that template modifies multiple pages in one swoop. At least that is what I am finding out so far.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Oct 05, 2017 12:16 am    Post subject: Reply with quote

Strider64 wrote:
{exclude}

Smile
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