Giter Club home page Giter Club logo

Comments (2)

kylekatarnls avatar kylekatarnls commented on June 4, 2024

Hi,

The easiest in your case would probably be a closure:

public function get() {
    $routerPathFor = array($this->router, 'pathFor');
    $pathFor = function ($pathName) use ($routerPathFor) {
        return call_user_func($routerPathFor, $pathName);
    };
    // Depending on your PHP version you can use directly $routerPathFor

   return $this->pug->render('productPage.pug', $this->templateParams, array(
      'pathFor' => $pathFor
   ));
}

You can also use share to get the function available in any template:

// somewhere in a global place after $this->pug is instanciated
// for example, the constructor of an abstract class that all controllers extend
$this->pug->share('pathFor', $pathFor);

With such a template:

a(href=$pathFor('foo'))=$pathFor('bar')

I hope it solve your problem.

from pug.

mcoenca avatar mcoenca commented on June 4, 2024

Perfect ! Well that means I need to dive in php closures a bit deeper...

Thanks for your very fast answer 👍

from pug.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.