Write PHP in Jade.
Gum is a Jade+JavaScript library that lets you write PHP with Jade. While not intended as a PHP replacement of any kind, Gum should offer a funky approach to most basic PHP programming tasks. See Installation and Samples sections below to learn how to see Gum in action.
Download with bower install gum
and start using it in your Jade projects with one line include gum/main
.
Clone to explore, modify and / or compile the code yourself. If you can help finding bugs and improving the library it's a ++!
git clone git://github.com/bucaran/gum.git
cd gum
npm install
gum.jade
is a mixin library that generates PHP. Think things like +if
, +while
, +for
, +each
, they are all there. Use +-
to declare and initialize variables, define and invoke functions or just echo out plain old PHP.
+-(meat="beef" countries=["Japan", "Argentina", "U.S"])
The above will register meat
and countries
as variables in the global namespace $
and echo the PHP to declare and initialize them. See $ below for more information.
There is also +- block
to echo the block of text inside <?php ?>
tags and
+-(before, after)
[block]
to output the block wrapped in before
and after
strings inside the <?php ?>
tags. Note that before
and after
are echoed even if no block is passed. Finally,
+-(function=myFunc a b[=c])
block
declares a PHP function myFunc
. The above will also add the myFunc
identifier as a Function object to $
.
There are several control structure-like mixins available in Gum that generate the corresponding PHP. Check the available documentation for more details.
+if(condition)
...
+elseif(condition)
...
+else
...
+fi
Echoes a PHP if
stament. Ideally we would do away with the closing +fi
but that means +elseif
and +else
would have to be nested inside the +if
.
+switch(condition)
+case(condition)
+case ...
+else
...
Switch
statement. Use +else
to handle default cases.
+for(i=0 to=10)
...
+for(j=10 to=0)
...
+for(k=100 to=-100 by=-10)
The above registers i
, j
and k
in $
and generates a for
loop that runs:
to
-from
times atby
intervals wherefrom
isi
,j
ork
.- If
by
is not set, the default will be 1 iffrom < to
, or -1 otherwise.
Similarly,
+each(in=array item)
registers item
in $
and echoes a foreach
loop that runs for each item in array
.
+while(condition)
...
Echoes a PHP while
statement.
We use blocks in Jade in order to implement mixins that nest and behave like the control flow structures we know and love. But without blocks to describe the logical conditions, variables or other extra parameters, we need to hack the arguments and attributes to make our mixins work akin to real control structures.
We can use regular strings to embed the PHP code and it will work, or we can use $
to create JavaScript objects that represent either functions or symbols; constants, variables, operators, etc., and link them together in a chain that returns the resulting PHP expression as a string.
$('getData')
+if($.getData('query'))
.success
+fi
The above adds a getData
Function object to $
that returns a PHP expression invoking getData([args, ...])
. To define variables, operators, etc., pass a second argument to $(name, value)
with the PHP output to echo when $.object
is evaluated.
In practice, it's easier to declare variables directly in Gum with +-
since that will generate the PHP as well.
This repository includes a samples/
directory where you can find the examples available for this release. In order to use Gum you need Jade and a PHP enabled server to run the generated samples/index.php
. Edit the samples.enabled
and samples.localhost
properties in the package.json
file to enable the samples.
"samples": {
"enabled": true,
"path": "samples/",
"localhost": "../path/to/your/localhost"
},
- Adds
main.jade
that loads gum. Including gum in your project it's easier nowinclude gum/main
.
- Adds
echo
andprint
to$.global.functions
.
- Adds
$.global.functions
to include some PHP functions by default:print_r
,var_dump
,printf
, etc. - Pass an array to
$(['f1', 'f2', 'f3'...])
to batch define a list of functions.
- Patches a versioning issue with bower.
- Initial Release.