Code Monkey home page Code Monkey logo

gum's Introduction

Gum v0.1.5 Built with Grunt

Write PHP in Jade.

What's this?

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.

Installation

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

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.

Variables & Functions

+-(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 $.

Control

There are several control structure-like mixins available in Gum that generate the corresponding PHP. Check the available documentation for more details.

If..elseif..else

+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

+switch(condition)
  +case(condition)

  +case ...

  +else
    ...

Switch statement. Use +else to handle default cases.

For

+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 at by intervals where from is i, j or k.
  • If by is not set, the default will be 1 if from < 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

+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.

Samples

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"
},

Get Social

Release History

0.1.5 / 2014-09-6
  • Adds main.jade that loads gum. Including gum in your project it's easier now include gum/main.
0.1.4 / 2014-09-5
  • Adds echo and print to $.global.functions.
0.1.2 / 2014-09-5
  • 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.
0.1.1 / 2014-09-4
  • Patches a versioning issue with bower.
0.1.0 / 2014-09-4
  • Initial Release.

By Jorge Bucaran

gum's People

Watchers

Adam J. Mendoza avatar

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.