Is it possible to write PHP in jade / pug?

Is it possible? If so, how? If this is not the case, do I need to give up the pug if I need to write PHP in my documents? After the search, I did not find anyone to notice this.

+5
source share
5 answers

You can embed PHP in Pug templates just like any plain text text you want to pass through relatively unmodulated [*]. There are several options described in the documentation , but I think these are the most suitable options for embedding PHP:

  1. After the item, it will just work. Eg p Good morning, <?php echo $user->name ?>.
  2. . "& lt;" , PHP (, <?php echo $foo; ?>) .
  3. PHP - , . HTML, Pug: , .

    p.
        <?php
        if ($multiline) {
            echo 'Foo!';
        }
        ?>
    

    , :

    |<?php
    |if ($multiline) {
    |   echo 'Foo!';
    |}
    |?>
    

    (, - 2 , , .)

  4. PHP , , : p(class!="<?php echo $foo ?>"). (, .)

, .pug .html, , PHP, . - gulp gulp-pug gulp-rename, :

var gulp = require('gulp'),
     pug = require('gulp-pug'),
     rename = require('gulp-rename');

gulp.task('default', function () {
    return gulp.src('./*.pug')
        .pipe(pug())
        .pipe(rename({
            extname: '.php'
        }))
        .pipe(gulp.dest('.'));
});

Pug, , , , , .

[*] Pug - , #{variable}, PHP.

+8

PHP , "" HTML - , PHP, , HTML. :

myPugTemplate.pug.php

html
  head
    title "<?= $this->title ?>"
  body
    <?php
      // Since we're outputing Pug markup, we have to take care of
      //   preserving indentation.
      $indent=str_repeat('  ', 2);
      if ($this->foo) {
        echo $indent . 'bar= myPost';
      } else {
        echo $indent . 'baz= myNav';
      }
    ?>
    footer
      +footerContent

Pug , Pug-, , Apache, mod_ext_filter, pug-cli :

ExtFilterDefine pug-to-html mode=output intype=text/pug outtype=text/html \
  cmd="pug"

<Location />
  SetOutputFilter pug-to-html
</Location>
+2

pug-php? , , , , : PHP Pug.

+1

scape :

!{'<?php #php code  ?>'}

:

p Hello !{'<?php echo "My name"; ?>'}

:

<p>Hello <?php echo "My name"; ?></p>

: https://pug-demo.herokuapp.com/

+1

Pug, PHP. Pug HTML, Pug, , PHP Pug. PHP Pug, :

Phug - Pug template engine for PHP

0
source

Source: https://habr.com/ru/post/1668727/


All Articles