Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to skip compiling for section of code

I have a jsrender template in my view which volt is currently trying to compile and obviously fails doing so. After some googling I'm very suprised/shocked to see that Volt apparently has no "literal" or "verbatim" block to just output raw content.

I found a function to add my own function to the compiler, but that doesn't support to add custom blocks (ie: {% literal %}{% endliteral %}).

How can I properly have this code in my view without applying a lot of ugly hacks? Or how can I add the literal-block myself to the compiler?

This is the piece of code that's giving errors right now, and I have quite a few other views which will have errors too.

<script id="uploaded-item-template" type="text/x-jsrender">
    <li data-uploadid="{{:id}}">
        <div class="uploaded-item">
            <p><div class="uk-badge {{:labelclass}}">{{:label}}</div> <strong>{{:title}}</strong></p>
            <div class="uk-progress {{:progressbarclass}}">
                <div class="uk-progress-bar" style="width: 100%;">{{:progressbartext}}</div>
            </div>
        </div>
    </li>
</script>


83.4k

i dont know what "literal" or "verbatim" are, but <?php ... ?> works in volt. hope that helps

I'm curious, could you please explain why You are using template engine instead of simple PHP file and Phalcon View component?

You need to learn Volt directives, which are merely just copies of PHP commands, and whole other bunch of things.

Plus it's additional overhead for memory and processor. What're gaining in exchange? Do You think it's worth it?



35.1k
Accepted
answer
edited Jul '19

Nothing stops you from using the pure PHP implementation with <?php echo $x; ?>.

The workflow is as follows:

  • You write your code in Volt.
  • Volt is memory resident so there is a negligible performance loss
  • Volt generates the PHP based templates
  • Your application uses those PHP templates to print the results on screen.

If you are in development mode, then the templates will be regenerated on every request (see compile directive when setting Volt up) but in production boxes, those templates (translation from Volt to PHP) is done only once and all that is stored on the server so there is pretty much no loss of performance.

The reason many people, myself included, use volt is because it is much cleaner and I don't have to type <?php echo $x; ?> compared to {{ x }}

If you are doing plain vanilla PHP/Volt/jQuery you won't have any issues. If you start injecting angulrjs, vue and other JS frameworks you might come up with collisions because some of those frameworks use the {{ and }} as placeholders for their functionality.

For now Volt indeed does not have the literal tag so that you can just inject something out there that you don't want Volt to parse it and also you cannot change the delimiters {{ and }}.

It is more of a convenience, ease in programming while keeping the performance as high as possible.

edited Jul '19

Well I have 20 yrs experience in web programming, and 100+ projects done, never used any template engine, and keep wondering why ppl are torturing themselves...

For me {{ x }} instead of <?php echo $x; ?> is not enough reason to learn several new directives, and occasionally meet additional problems you've mentioned above.

So my question was a little bit of provocation, cause I had hope, maybe there are some new reasons, but no :)

The only legit reason I see, that make sense sometimes, is security - when you have to allow for example a designer to make changes in template, but, it's rare, and can be resolved by other means.

FIrst of all, it seems to me that you're mixing a frontend and backend template framework in the same file. If that's truly the case, you're basically making life hard for yourself, it has nothing to do with the technologies. You shouldn't even enounter a problem like this if you lay out your project technologies properly.

Well I have 20 yrs experience in web programming, and 100+ projects done, never used any template engine, and keep wondering why ppl are torturing themselves...

20 years ago we were hacking with mysql_real_escape (or that func came out even later), you shouldn't get stuck at age-old practices.

Template engines are great, because they focus your concern and are less verbose. You only PRESENT data in the form of views, without any application logic. That leads to maintainable and flexible code.

You could also write modern, complex web applications in C or Assembly, and then you only need to know 1 language. I hope you find this notion ridiculous aswell.

It is your choice how you program. Some people like Volt, Mustache, Twig (to name a few templating engines) some don't.

The latest push I see is to not use any templating engine whatsoever and use Phalcon and other PHP frameworks as a backend only, sending data back to the UI using JSON as an API. As they say in the US whatever floats your boat :)

edited Jul '19

20 years ago we were hacking with mysql_real_escape (or that func came out even later), you shouldn't get stuck at age-old practices.

Template engines are great, because they focus your concern and are less verbose. You only PRESENT data in the form of views, without any application logic. That leads to maintainable and flexible code.

Thats completly wrong, my pure php templates have minimal or zero php logic, just <?php echo $var ?> with optional html escape if i need it. Not using php template engine doesn't mean i'm stuck with old things, it's Your wrong assumption.

PHP itself is much of a template engine (when compared for example to Java / Python / .NET), no need to put another template engine on top of existing template engine.

Still you haven't presented any logic argument to use Volt or other TE for PHP.

I can do exactly the same ("PRESENT data in the form of views, without any application logic.") without template engine. TE is just unneccesary burden, one of many bad things that exists just because it's popular not because it's good - IMO

edited Jul '19

It is your choice how you program. Some people like Volt, Mustache, Twig (to name a few templating engines) some don't. [...]

Exactly, there is no technical reason to use TE, just a habit or preference.

Thats completly wrong, my pure php templates have minimal or zero php logic, just <?php echo $var ?> with optional html escape if i need it. Not using php template engine doesn't mean i'm stuck with old things, it's Your wrong assumption.

Logic is the least of your problems, the main conceptual issue is that you are mixing Volt and JSRender in the SAME file.

PHP itself is much of a template engine (when compared for example to Java / Python / .NET), no need to put another template engine on top of existing template engine.

*was a template engine. Yet again, 20 years ago that sentiment would've been mostly correct, but things changed.

Still you haven't presented any logic argument to use Volt or other TE for PHP.

Template engines are great, because they focus your concern and are less verbose. That leads to maintainable and flexible code.

This one reason is more than enough to use template engines

"Template engines are great, because they focus your concern and are less verbose. That leads to maintainable and flexible code."

You can do exactly the same without TE, and BTW - being verbose is actually big plus in webdevelopment

"Template engines are great, because they focus your concern and are less verbose. That leads to maintainable and flexible code." You can do exactly the same without TE, and BTW - being verbose is actually big plus in webdevelopment

Syntactic verbosity is never a good thing. anywhere (*looks at lisp accusingly*). Sure you can acheive the same RESULT without a template engine, the difference is in code management.

As I've mentioned, you could write your app in ASM, but your and your teammates' productivity will suffer.

But hey, as @Nikolaos have said, whatever floats your boat... we are no convention-police or anything :P

edited Jul '19

It's not about result, it's about mere fact:

There is no difference between template in pure php (Phalcon View component + phtml file) and any template engine, it's only about decision whether you're putting php logic into template or not, doesnt matter what kind of template it is.

I saw spaghetti code in templates written in various TE's, it really doesnt matter, it's about culture of Your coding.

it's only about decision whether you're putting php logic into template or not, doesnt matter what kind of template it is. I saw spaghetti code in templates written in various TE's, it really doesnt matter, it's about culture of Your coding.

You're still confusing business logic with syntax and readability. They are separate issues, there should be no question whatsoever whether a template ought to contain business logic, let it be phtml or volt.

I understand where you're coming from, I can't be bothered to learn CoffeeScript / TypeScript, give me my ES6 and I'm fine. But even I admit that both those supersets have advantages over plain JS.

I can achieve the same RESULT, but the maintainability of my code would be implicitly better, because those languages force conventions on you.

Ok, so you are no longer saying that separating Logic from View require TE, that's a progress.

Now You're saying that TE is just more readable, and that is questionable, because if You do not use any logic inside template, and just presenting variables and doing iterations, there is not much of a difference between readability, it's just matter of a preference :)

Preference that cost You learning new directives that are merely copies of PHP directives.

Habit is a powerful thing, If you're used to Volt, it's great for You, but put Yourself in position of PHP beginner - he must learn a lot of things, and putting on him another one, is just IMO unneccesary burden, justified by someone's preference.

Wow, you are one dense specimen. Sorry, but my patience is at it's end. I probably shouldn't have replied, but here it goes:

Ok, so you are no longer saying that separating Logic from View require TE, that's a progress.

I never said that only TE separates business logic. I said that TEs ENCOURAGE separation of presenataion and business logic.

Now You're saying that TE is just more readable, and that is questionable, because if You do not use any logic inside template, and just presenting variables and doing iterations, there is not much of a difference between readability, it's just matter of a preference :)

You're wrong. Template engines are DESIGNED to be less verbose (syntactically) than their host language counterparts. And I haven't even mentioned some utility facilities, like filters. Oh sure, you can implement your own in plain PHP, but why bother if there are readily available solutions off the shelf?

Preference that cost You learning new directives that are merely copies of PHP directives. Habit is a powerful thing, If you're used to Volt, it's great for You, but put Yourself in position of PHP beginner - he must learn a lot of things, and putting on him another one, is just IMO unneccesary burden, justified by someone's preference.

This is the 3rd time I mention this, maybe this will be the charm: If you are too lazy to learn new technologies, why not write all your code in ASM?

But the crux of your original problem was that you are mixing JSRender and Volt, and both of them use mustaches as special characters. JSRender is a frontend template technology, Volt is a backend template technology. Why you would even try to mix these in the same file is beyond me.

Which probably proves my point, that a good understanding of TEs is a must in modern web-development.

Keep it civil please. Opinions are like butts. Everyone has one.

"You're wrong. Template engines are DESIGNED to be less verbose (syntactically) than their host language counterparts"

And what are You gaining by being less verbose syntactically? Does it make templates more readable? That's questionable....

And no, I'm not lazy, but why should I learn something which I consider useless for me? There are houndreds of additional packages and solutions for PHP, should I learn them all, or just those packages You're consider useful? ;)

Okay I thinks there is no sense in continuing this, have a nice day :)

Well that was an entertaining read througout the day, at least it's good to know there's an active community here haha.

Add function to you compiler like:

$compiler->addFunction('ngb', function ($resolvedArgs, $exprArgs) use ($compiler) {
                        $value = $compiler->expression($exprArgs[0]['expr']);
                        return '"{{".'.$value.'."}}"';
                    });

And just use {{ ngb(':labelclass:') }} in volt, in php file it will otuput {{ :labelclass }}. You can rename it to whatever you want.