Layout-based PHP micro-framework for full-stack HTML5 sites
Hackwork is a layout-based PHP micro-framework made for HTML5 sites. You can also make HTML4 sites with Hackwork, don't worry.
Minimal required PHP version is 5.6.0. Hackwork may work on older PHP versions, although they aren't supported officially.
- Example
- Getting started
- Directory structure
- Core
- Configuration
- Layouts
- HTTP
- Errors
- Contributing
- License
<?php
require_once 'core/hackwork.php';
layout('default', 'home');
- Clone the repository.
- Modify layouts for your pages.
- Fill
data/
andassets/
. - Test your site locally to see does everything works well.
Hackwork projects should have a simple directory structure.
.
├── assets/
│ ├── css/
│ ├── fonts/
│ ├── img/
│ ├── js/
│ ├── ...
├── core/
│ ├── helpers/
│ ├── hackwork.php
├── data/
│ ├── ...
└── layouts/
└── .../
├── footer.php
├── header.php
├── i.functions.php
├── i.variables.php
└── ...
assets/
: cachable resources (e.g. JavaScript, CSS and images).core/
: framework coredata/
: pages contentlayouts/
: base layouts directory
There are path and configuration constants.
ROOT
: server root path; don't change if not necessaryPATH
: site root pathCORE
: framework core path,PATH
-relativeHELPERS
: helpers path,CORE
-relativeLAYOUTS
: layouts path,PATH
-relativeDATA
: data files path,PATH
-relativeASSETS
: assets path, isn'tPATH
-relativeCSS
: CSS assets path,ASSETS
-relativeJS
: JavaScript assets path,ASSETS
-relativeFONTS
: fonts path,ASSETS
-relativeIMG
: images path,ASSETS
-relative
You should omit trailing slashes in path constants.
There is an environment constant, ENV
. Its values are:
development
for complete error reportingproduction
for minimal error reporting
By default it's development
. You should change it if your site is for
production.
Hackwork has some helpers, e.g. to make layout. All of them are automatically
imported to core/hackwork.php
.
Server configuration is in the core/helpers/config.php
. You should edit it to
adjust the configuration.
Pages loads faster with compression, so Hackwork enables it by default.
There is a charset definition to ensure right charset is used everywhere. Hackwork sets default charset to UTF-8.
PHP requires default timezone for proper working of time functions. Hackwork sets default timezone to UTC.
Hackwork uses layouts as page generating model.
Layouts are like page templates. You don't need to learn new templating language as Hackwork uses pure PHP syntax.
header.php
: page top- page content (loaded from
data/
) footer.php
: page bottom
Use i.
prefix for layout files that should be included. Core i.
files are:
i.variables.php
: layout variablesi.functions.php
: layout functions
You can create additional i.
files, e.g. for constants and classes.
To generate layout, use layout($layout, $data, $page_title)
function.
Default layout is just a template. It lies within layouts/default/
.
Default layout variables are in layouts/default/i.variables.php
.
$doctype
: document type
$charset
: character set$meta
:<meta>
tags content array$meta['site_title']
: site title$meta['author']
: site author$meta['description']
: site description$meta['keywords']
: site keywords separated with comma$meta['robots']
: robots meta setting$meta['viewport']
: visible part of canvas at page
$title_divider
: divider between page and site title$title
: generated title
$stylesheet
:<link rel="stylesheet">
tags content array$icon
:<link rel="*icon">
tags content array$icon['favicon']
: favicon path$icon['apple_touch_icon']
: Apple touch icon path
$script
:<script>
tags content array
$cpsign
: copyright sign$cpyear
: first copyright year$cpowner
: copyright owner$copyright
: copyright text
Default layout functions are in layouts/default/i.functions.php
.
make_meta($array)
: generates<meta>
tags from given arraymake_stylesheets($array)
: generates<link rel="stylesheet">
tags from given arraymake_icons($array)
: generates<link rel="*icon*">
tags from given arraymake_scripts($array)
: generates<script>
tags from given array
is_currentfile($file)
: checks is the given argument current filefilecount($dir, $ignore)
: counts files in a directorycat($url, $pre)
: imitatescat
Unix commandrandomval($array)
: selects a random value from arrayundot($string)
: removes dots from string
To make a new layout, create a new directory within layouts/
and follow
layout basics. You can use default layout
as template.
Hackwork caches HTTP properties and header messages for easier HTTP control.
$httpv
: HTTP version; don't change if not necessary
$header
is an array of default HTTP header messages. You can use headers with
$header[<status-number>]
.
Hackwork has a simple error thrower.
EXIT_SUCCESS
: no errorsEXIT_ERROR
: generic errorEXIT_CONFIG
: configuration errorEXIT_UNKNOWN_FILE
: file not foundEXIT_UNKNOWN_CLASS
: unknown classEXIT_UNKNOWN_METHOD
: unknown class memberEXIT_USER_INPUT
: invalid user inputEXIT_DATABASE
: database errorEXIT_AUTO_MIN
: minimal automatically-assigned error codeEXIT_AUTO_MAX
: maximal automatically-assigned error code
To throw an error, set consistent headers and terminate script, use
throwerr($header_status, $exit_status, $msg, $header_msg)
.
Want to contribute? Check out contributing guide.
MIT © Zlatan Vasović