pug-php / pug

Pug template engine for PHP
https://www.phug-lang.com
MIT License
391 stars 42 forks source link

Use javascript library in template #170

Closed MarcelRobitaille closed 7 years ago

MarcelRobitaille commented 7 years ago

Hello. Is it possible to use a javascript library (moment, for example) in a template? I know I can pass php closures but I don't know how to pass javascript functions. Maybe we could extend $pug->share in some way?

Thanks!

kylekatarnls commented 7 years ago

You will have different possible approaches:

MarcelRobitaille commented 7 years ago

When I try to require('moment') I get require is not defined even though I have pugjs set to true in the configuration. What do you mean by "If node is available". Node is installed on my computer if that's what you mean.

The reason for a js library with php is that I suspect the project will get moved to node quite soon.

Sorry for the late reply.

kylekatarnls commented 7 years ago

It seems related to this: https://github.com/pugjs/pug/issues/1810 I will try to find a work-around.

The reason for a js library with php is that I suspect the project will get moved to node quite soon.

It's a good reason. and so pugjs set to true is the best way to get closest to node as possible.

MarcelRobitaille commented 7 years ago

Thanks for the info.

kylekatarnls commented 7 years ago

We will be able to make require() available inside templates rendered with pugjs. This will be available with the version 3.0.1.

kylekatarnls commented 7 years ago

As it finally contains minor changes, it's the version 3.1.0, you can update to test it

MarcelRobitaille commented 7 years ago

I have upgraded to 3.1.0 and now I am getting Cannot find module 'moment'.

kylekatarnls commented 7 years ago

How did you install moment? Did you use npm -g install moment running with the same user that one that run PHP? Or did you use the composer setting:

"extra": {
  "npm": {
    "moment": "^2.19.1"
  }
}

And run composer install?

MarcelRobitaille commented 7 years ago

I installed it through composer as you described.

kylekatarnls commented 7 years ago

I found a solution, I will update nodejs-php-fallback to expose its own require method. I'll get you in touch.

kylekatarnls commented 7 years ago

Hi, please try to composer update, install with extra.npm composer section should now works with pugjs option to true.

MarcelRobitaille commented 7 years ago

I ran composer update and I am still getting the same errors. Is there anything special I have to do to install the node packages also?

kylekatarnls commented 7 years ago

You should just need to have in your composer.json:

"extra": {
  "npm": {
    "moment": "^2.19.1"
  }
}

And when you do composer update, you should see Package added to be installed/updated with npm: moment

Or you can install it manually in nodejs-php-fallback by running:

<?php

require 'vendor/autoload.php';

\NodejsPhpFallback\NodejsPhpFallback::installPackages(['moment' => '^2.19.1']);
MarcelRobitaille commented 7 years ago

That's exactly what I did.

composer.json:

{
  "require": {
    "pug-php/pug": "3.1.0"
  },
  "extra": {
    "npm": {
      "moment": "2.19.1"
    }
  }
}

composer update:

Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
The node package [jstransformer] can be installed:
It allows you to use any jstransformer npm package as filter by adding it in your extra.npm section of composer.json.
Would you like to install/update it? (if you're not sure, you can safely press Y to get the package ready to use if you need it later) [Y/N] y
The node package [pug-cli] can be installed:
It allows you to use the native JS pug engine instead of the default PHP one with the `pugjs` option set to `true`
Would you like to install/update it? (if you're not sure, you can safely press Y to get the package ready to use if you need it later) [Y/N] y
Package added to be installed/updated with npm: jstransformer@"^1.0.0"
Package added to be installed/updated with npm: pug-cli@"^1.0.0-alpha6"
Package added to be installed/updated with npm: moment@"2.19.1"
Packages installed.
kylekatarnls commented 7 years ago

You must upgrade to pug-php 3.1.1.

MarcelRobitaille commented 7 years ago

Right. I am dumb sometimes. Thanks for all the help!