This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

allw
allw

hi all,

i have been trying to find a solution to needing to trigger php code using twig (i only get the information as the form runs) and need to test it for an error 404.

curl_setopt($priv_URL, CURLOPT_RETURNTRANSFER, true);
curl_exec($priv_URL);
if(curl_getinfo($priv_URL, CURLINFO_HTTP_CODE) == 404) {
return true;
}else {
return false;
}
curl_close($prv_URL);

here is the incredibly simple way i have come up with of testing if the link creates a 404 error ( i know not 100% fool proof) so far i have tried just creating a php function like function is404check($priv_URL){ //..} and from looking on the twig website i found

$twig = new Twig_Environment($loader);
$function = new Twig_SimpleFunction('is404check', function ($priv_URL) {
//..
});
$twig->addFunction($function);

but this throws errors first at the $twig = new twig... and if i remove that, as expected at $twig->addfunction...

how do i overcome this issue in octobercms?

thanks!

Last updated

UpTop
UpTop

You have to register Twig functionality in your plugin. Check This post for a possible solution.

Also: https://octobercms.com/docs/plugin/registration#extending-twig

Last updated

allw
allw

but i don't want to create a plugin every time i want to add a custom function to a page? why can i not just define it on my "code" section of a page?

Art and Code Studio
Art and Code Studio

You can define functions in the theme code section with Anonymous Class, but they are only working on PHP7:

// in Code
function onStart()
{
    // Anonymous Class only working on PHP7
    $this['code'] = new class {
        public function foo() {
            return 'Hello World!';
        }
    };
}
{# in twig Markup #}
{{ code.foo() }}

Last updated

Miguel Debruyne
Miguel Debruyne

So how can I define a function thats accessible everywhere?

I want a better print function, because when I'm using the {{ dump(variabe) }} it only gives me a list of functions. I want to see the key -> values in a nice way. Seems like this isn't native in october cms or am I missing something?

function debug($data, $die = false)
{
    echo "<pre>";
    print_r($data);
    echo "</pre>";

    if ($die) {
        die();
    }
}

Last updated

Eoler
Eoler

Miguel Debruyne said: So how can I define a function thats accessible everywhere? I want a better print function, because when I'm using the {{ dump(variabe) }} it only gives me a list of functions. I want to see the key -> values in a nice way. Seems like this isn't native in october cms or am I missing something?

You're missing custom Twig markup tags registration: https://octobercms.com/docs/plugin/registration#extending-twig

Relevant debugging example: https://github.com/inetis-ch/oc-dump-plugin

Last updated

Miguel Debruyne
Miguel Debruyne

Eoler said: You're missing custom Twig markup tags registration: https://octobercms.com/docs/plugin/registration#extending-twig

Relevant debugging example: https://github.com/inetis-ch/oc-dump-plugin

But it's still only accessible on pages where the plugin/component is active, no? What about for example a logging function that I could use in literally every php file.

Thank you for that dump-plugin tho, seems usefull!

Eoler
Eoler

Miguel Debruyne said: But it's still only accessible on pages where the plugin/component is active, no? What about for example a logging function that I could use in literally every php file.

No, custom filters and functions are available everywhere Twig engine is initialized (plugin must be enabled, of course).

esawi44269193
esawi44269193

Art and Code Studio said:

You can define functions in the theme code section with Anonymous Class, but they are only working on PHP7:

// in Code
function onStart()
{
   // Anonymous Class only working on PHP7
   $this['code'] = new class {
       public function foo() {
           return 'Hello World!';
       }
   };
}
{# in twig Markup #}
{{ code.foo() }}

is this the best way to achieve the goal? is there a newer way now?

1-9 of 9

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.