jade-php icon indicating copy to clipboard operation
jade-php copied to clipboard

Variable in attribute

Open bernex opened this issue 9 years ago • 5 comments

How to do this:

<a class="<?php echo $var; ?>"Google</a>

May be from this:

a(class=$var) Google

But don't work.

Thanks!

bernex avatar Oct 26 '14 10:10 bernex

Slightly un-shorthand, but you can use eg. a(foo!="<?php echo $bar ?>") - ie. it will print the unescaped string "" in the attribute.

bgilhome avatar Nov 04 '14 22:11 bgilhome

Also just found out that if you want to echo php directly into the tag (not in an attribute) you can use multiline format, eg:

a(foo!="" )

(second line should be indented)

EDIT: this seems to only work with a doctype declaration at top of file, eg. !!!5

bgilhome avatar Nov 04 '14 23:11 bgilhome

It would be great if this functionality were built in.

M-Pixel avatar Feb 07 '15 04:02 M-Pixel

+1, that would be a nice feature indeed

Larzans avatar Feb 25 '15 14:02 Larzans

I have a better way:

Add a local variable into the compiler function wherever you have it, for example in gulp-jade you might find it inside:

gulp.src("some/src")
.pipe(jade({locals:{some:"value"}}))

This variable is going to be a function that can be called in all of your jade files. Lets focus on the jade function as seen above inside a pipe in that example.

JS new function:

jade(locals:{
        $:function(str){
            return "<?php print $"+str+" ?>"
        }
    })

JADE usage:

h1(class!=$(php_variable))

COMPILES

<h1 class="<?php print php_variable?>"></h1>

adam-cyclones avatar Sep 06 '15 21:09 adam-cyclones