Giter Club home page Giter Club logo

Comments (4)

kylekatarnls avatar kylekatarnls commented on June 19, 2024

functionsNamespace option added in js-phpize/js-phpize 2.6.0

So you can run composer update then now use:

namespace myNameSpace;

function fooBar() {
  return 'Hello';
}

$pug = new \Pug\Pug([
    'module_options' => [
        'jsphpize' => [
            'functionsNamespace' => 'myNameSpace',
        ],
    ],
]);

$pug->display('p=fooBar()');

This is the new recommended way to call the functions from a custom namespace in templates.

from pug.

windware-ono avatar windware-ono commented on June 19, 2024

Hi.

Thanks for a quick resolution. However, I still cannot apply it given the following code.
Also, you misspelled functionsNamespace as functionNamespace in the example code above.

Your code works but perhaps it's because I'm calling it from a static method of a class?

<?php
namespace phugNamespace;
require('vendor/autoload.php');

class namespaceTest
{
	function __construct()
	{
		$pug = new \Pug\Pug([
			'module_options' => [
				'jsphpize' => [
					'functionsNamespace' => 'phugNamespace',
				],
			],
		]);

		print $pug->render('div=namespaceTest::templateMethod()');
	}
	
	static function templateMethod()
	{
		return 'Hello';
	}
}

new namespaceTest();

If I change div=namespaceTest::templateMethod() into div=\phugNamespace\namespaceTest::templateMethod() , then it will render fine.

I'm using php 7.4.0 and composer.json being,

{
	"require" : {
		"pug-php/pug" : "^3.0"
	}
}

which installed the following.

js-phpize/js-phpize                     2.6.0
js-phpize/js-phpize-phug                2.2.0
js-transformer/js-transformer           1.0.0
nodejs-php-fallback/nodejs-php-fallback 1.4.1
phug/js-transformer-filter              1.2.0
phug/phug                               1.4.0
pug-php/pug                             3.3.1
symfony/polyfill-mbstring               v1.13.1
symfony/var-dumper                      v5.0.1

from pug.

kylekatarnls avatar kylekatarnls commented on June 19, 2024

functionsNamespace is for functions not for methods. I'm not sure we should allow calls of static class methods from inside the template in an easier way than \phugNamespace\namespaceTest::templateMethod()

It sounds to me like mixing responsibilities between your controller and your view and some kind of false OOP.

In your case, I recommend this approach:

<?php

namespace phugNamespace;

require 'vendor/autoload.php';

class namespaceTest
{
    public function __construct()
    {
        (new \Pug\Pug)->display('div=view.templateMethod()', [
            'view' => $this,
        ]);
    }

    public static function templateMethod()
    {
        return 'Hello';
    }
}

new namespaceTest();

from pug.

windware-ono avatar windware-ono commented on June 19, 2024

Ok, thanks. I've changed my code to work like that.

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.