This is an outdated version of PHPasap. Please visit http://phpasap.com/docs for the latest version.

HTML Helper for handling Assets

Instead of hardcoding the absolute URL or using a relative URL for loading a resource (js, css and image file) you are strongly encouraged to use below methods as needed. These will take care of prepending the base URL as needed so that you don't have to worry while deploying your application from development to production server.

Adding a stylesheet

echo HTML::style("assets/css/style.css");
//assets folder is in project root and not necessarily web root
Above tag will produce <link rel="stylesheet" href="http://your domain.com/assets/css/style.css" >. PHPasap will prepend the base url to the provided url path.

Adding an external stylesheet

echo HTML::style("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/styles/googlecode.min.css");

The syntax for loading an external stylesheet is same as for loading a local one. There is no magic here. Whenever there is a http:// or https:// PHPasap assumes that provided path is an absolute path and hence doesn't prepend the base url to the provided path.

However in some case you may want to explicitly specify if the resource is local or external instead of letting PHPasap do the guessing. Below is a particular use case

echo HTML::style("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/styles/googlecode.min.css", true);

In above example a boolean true in second parameter tells PHPasap that this is an external resource. The default value for this parameter is boolean false. By default PHPasap will assume the resource is a local one if provided path doesn't begin with http:// or https://.

Adding a script

echo HTML::script("assets/js/common.js");
//assets folder is in project root and not necessarily web root

Usage is same as HTML::style() above

Adding an external script

echo HTML::script("https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js");

echo HTML::script("//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js", true);
//by giving true in second param we are telling PHPasap this is external resource. See HTML::style() for more info

Creating a link

echo HTML::link("news/all", "All News");

The above will produce <a href="http://some-domain.com/news/all">All News</a>

Creating an external link

echo HTML::link("http://perials.com", "Perials Website");
//<a href="http://perials.com">Perials Website</a>

Whenever the provided url path starts with a http:// or https:// PHPasap assumes it as a external resource and doesn't prepend base url to it.

Just as with HTML::style() and HTML::script() passing second param as true tells PHPasap to treat this as external resource irrespective of what the url passed in first param is.

echo HTML::link("//perials.com", "Perials Website", true);
//<a href="http://perials.com">Perials Website</a>

Adding custom attributes

The optional third param for HTML::style() and HTML::script() and fourth param for HTML::link() is an associative array. Anything in this array will be added as a string of attr="value" to the respective resource tag. See below examples

echo HTML::link("news/all", "All News", false, ["class"=>"anchor", "attr2"=>"value2"]);
//<a href="http://some-domain.com/news/all" class="anchor" attr2="value2">All News</a>

echo HTML::style("assets/css/style.css", false, ["attr1"=>"value1", "attr2"=>"value2"]);
//<link rel="stylesheet" href="http://some-domain.com/assets/css/style.css" attr1="value1" attr2="value2" >

echo HTML::script("assets/js/common.js", false, ["attr1"=>"value1", "attr2"=>"value2"]);
//<script src="http://some-domain.com/assets/js/common.js" attr1="value1" attr2="value2"></script>

Creating urls

echo HTML::url("news/all");

Above code will produce http://some-domain.com/news/all

PHPasap doesn't support external url (say http://google.com) as you are better off hardcoding it as it is.