phug-php / phug

Phug - The Pug Template Engine for PHP
https://phug.selfbuild.fr
MIT License
62 stars 3 forks source link

Where is range() function? How to add extension function? #43

Closed OnkelTem closed 5 years ago

OnkelTem commented 5 years ago

I need the range() function which for some reason is not there already. So I'd like to create one but I couldn't found anything helping in the docs. Could you please show an example of adding a function?

kylekatarnls commented 5 years ago

https://www.php.net/manual/en/function.range.php

The native PHP function works just fine. Why not using it?

kylekatarnls commented 5 years ago
ul
  each val in range(1, 10)
    li= val
OnkelTem commented 5 years ago

Oh, really? Come on! Didn't know we can use native functions. But wait, isn't it unsafe?

Apart from that, is there still a way to add an extension function?

kylekatarnls commented 5 years ago

Unsafe? coding pug code is the same "safety" level as coding in PHP (or in JS for pugjs). As long as you don't render pug code that has been typed by untrusted user, there's no risk.

There are several ways to use PHP functions inside your template: first create a global function and include it before render. You also pass it as a closure as a view variable or using Phug::share for global use. See https://phug-lang.com/#shared-variables-globals-array

Or you can share a object that handle the methods you want as methods.