Page 1 of 1

Why switch to Twig and not include basics like "asset" and "Mobile Detect"?

Posted: Wed Oct 11, 2017 4:44 am
by sitedeveloper999
I'm a little perplexed. OpenCart developers decided to switch to the Twig language for the templates, instead of the good old PHP that everyone's familiar with.
OK, that's fine, couple of days of reading manuals and I figured out most of the things I need to change.

But why couldn't they include basic functions like, well, INCLUDING assets like stylesheets? Or bundling the Mobile Detect function?
It's not like over 45% of users are mobile, and we MIGHT want to have the option to include different things on pages, depending on whether the user is desktop or mobile...
...and it's not like it would be convenient to include separate CSS files or other code-blocks, instead of trying to cram everything into one template file...

So yeah. Trying to use the "asset" command in a template, results in the following error:

Uncaught exception 'Twig_Error_Syntax' with message 'Unknown "asset" function in "journal2/template/product/product.twig" at line 1.

What's the solution?

Re: Why switch to Twig and not include basics like "asset" and "Mobile Detect"?

Posted: Wed Oct 11, 2017 6:56 am
by yodapt

Re: Why switch to Twig and not include basics like "asset" and "Mobile Detect"?

Posted: Wed Oct 11, 2017 9:38 am
by IP_CAM
What's the solution?
Well, one of them could be, to stay with what one already knows, like me and my XP,
if one does not want to learn, again and again, just to keep on doing, what one has
been so familiar with before ... :laugh:
And one cannot survive, on longer terms, by always doing the same, that's Business !
Ernie

( PS: Especially, since MS re-activated the official Upgrade-Gate again for XP`s ;) )

Re: Why switch to Twig and not include basics like "asset" and "Mobile Detect"?

Posted: Fri Oct 20, 2017 4:56 am
by sitedeveloper999
"Google is your friend"... with a link that explains how to do this in PHP.

I know how to do this in PHP.
I have it done in PHP.
But in case you didn't notice, PHP code doesn't work in Twig templates.

And if I try to use the solution/s listed online, they require the "Mobile Detect" library to be installed. Which isn't included.

That's my point. With half the users out there being on mobile browsers, why not include a mobile-detect library in the distro, so developers can have the SAME functionality as what they get with the PHP libraries installed on most hosting servers?

Re: Why switch to Twig and not include basics like "asset" and "Mobile Detect"?

Posted: Fri Oct 20, 2017 4:59 am
by sitedeveloper999

Code: Select all

<?php
          $ismobile = check_user_agent('mobile');
          if($ismobile) {
            echo('<div class="mobile_wrapper">');
                include('product-block-mobile-version.php');
            echo('</div>');   
          } else {
            echo('<div class="desktop_wrapper">');
                include('product-block-desktop-version.php');
            echo('</div>');   
          }
?>
Now, can we do the same in Twig?

Try it, and discover that the basic function "check_user_agent" is NOT THERE.

Unless it's called something else entirely?

Re: Why switch to Twig and not include basics like "asset" and "Mobile Detect"?

Posted: Fri Oct 20, 2017 6:30 am
by yodapt
You dont. You do that in the controller, in PHP, like I said before.