Ejemplo n.º 1
0
 function render_template($name)
 {
     $tmp_dir = Wordless::theme_temp_path();
     $template_path = Wordless::join_paths(Wordless::theme_views_path(), "{$name}.haml");
     if (!is_file($template_path)) {
         render_error("Template missing", "<strong>Ouch!!</strong> It seems that <code>{$template_path}</code> doesn't exist!");
     }
     if (!file_exists($tmp_dir)) {
         mkdir($tmp_dir, 0760);
     }
     if (!is_writable($tmp_dir)) {
         chmod($tmp_dir, 0760);
     }
     if (is_writable($tmp_dir)) {
         $haml = new HamlParser(array('style' => 'expanded', 'ugly' => false));
         include $haml->parse($template_path, $tmp_dir);
     } else {
         render_error("Temp dir not writable", "<strong>Ouch!!</strong> It seems that the <code>{$tmp_dir}</code> directory is not writable by the server! Go fix it!");
     }
 }
Ejemplo n.º 2
0
 /**
  * Renders a template and its contained plartials. Accepts
  * a list of locals variables which will be available inside
  * the code of the template
  *
  * @param  string $name   The template filenames (those not starting
  *                        with an underscore by convention)
  *
  * @param  array  $locals An associative array. Keys will be variables'
  *                        names and values will be variable values inside
  *                        the template
  *
  * @see php.bet/extract
  *
  */
 function render_template($name, $locals = array())
 {
     $valid_filenames = array("{$name}.html.haml", "{$name}.haml", "{$name}.html.php", "{$name}.php");
     foreach ($valid_filenames as $filename) {
         $path = Wordless::join_paths(Wordless::theme_views_path(), $filename);
         if (is_file($path)) {
             $template_path = $path;
             $arr = explode('.', $path);
             $format = array_pop($arr);
             break;
         }
     }
     if (!isset($template_path)) {
         render_error("Template missing", "<strong>Ouch!!</strong> It seems that <code>{$name}.html.haml</code> or <code>{$name}.html.php</code> doesn't exist!");
     }
     extract($locals);
     switch ($format) {
         case 'haml':
             $tmp_dir = Wordless::theme_temp_path();
             if (!file_exists($tmp_dir)) {
                 mkdir($tmp_dir, 0760);
             }
             if (!is_writable($tmp_dir)) {
                 chmod($tmp_dir, 0760);
             }
             if (is_writable($tmp_dir)) {
                 $haml = new HamlParser(array('style' => 'expanded', 'ugly' => false));
                 include $haml->parse($template_path, $tmp_dir);
             } else {
                 render_error("Temp dir not writable", "<strong>Ouch!!</strong> It seems that the <code>{$tmp_dir}</code> directory is not writable by the server! Go fix it!");
             }
             break;
         case 'php':
             include $template_path;
             break;
     }
 }
Ejemplo n.º 3
0
 public static function compile_assets()
 {
     foreach (self::$preprocessors as $preprocessor) {
         foreach ($preprocessor->supported_extensions() as $extension) {
             $list_files = self::recursive_glob(self::theme_assets_path(), "*.{$extension}");
             foreach ($list_files as $file_path) {
                 // Ignore partials
                 if (!preg_match("/^_/", basename($file_path))) {
                     $compiled_file_path = str_replace(Wordless::theme_assets_path(), '', $file_path);
                     $compiled_file_path = Wordless::join_paths(Wordless::theme_static_assets_path(), $compiled_file_path);
                     $compiled_file_path = preg_replace("/\\." . $extension . "\$/", "." . $preprocessor->to_extension(), $compiled_file_path);
                     try {
                         $to_process_file_path = preg_replace("/\\." . $extension . "\$/", "", $file_path);
                         $compiled_content = $preprocessor->process_file_with_caching($to_process_file_path, Wordless::theme_temp_path());
                     } catch (WordlessCompileException $e) {
                         echo "Problems compiling {$file_path} to {$compiled_file_path}\n\n";
                         echo $e;
                         echo "\n\n";
                     }
                     file_put_contents($compiled_file_path, $compiled_content);
                 }
             }
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * If we get back our custom query vars, then redirect the request to the preprocessor.
  */
 public static function parse_request(&$wp)
 {
     foreach (self::$preprocessors as $preprocessor) {
         if (array_key_exists($preprocessor->query_var_name(), $wp->query_vars)) {
             $original_url = $wp->query_vars[$preprocessor->query_var_name('original_url')];
             $relative_path = str_replace(preg_replace("/^\\//", "", self::theme_url()), '', $original_url);
             $processed_file_path = Wordless::join_paths(get_template_directory(), $relative_path);
             $relative_path = preg_replace("/^.*\\/assets\\//", "", $relative_path);
             $to_process_file_path = Wordless::join_paths(self::theme_assets_path(), $relative_path);
             $to_process_file_path = preg_replace("/\\." . $preprocessor->to_extension() . "\$/", "", $to_process_file_path);
             $preprocessor->process_file_with_caching($to_process_file_path, $processed_file_path, Wordless::theme_temp_path());
             exit;
         }
     }
 }
Ejemplo n.º 5
0
 /**
  * Resizes the specified image to the specified dimensions.
  * 
  * The crop will be centered relative to the image.
  * 
  * @param string $src
  *   The path to the image to be resized
  * @param int $width
  *   The width at which the image will be cropped
  * @param int $height
  *   The height at which the image will be cropped
  * 
  * @return string
  *   The valid URL to the image
  * 
  * Cropped images are stored in template tmp folder. 
  * 
  * @ingroup helperfunc
  */
 function resize_image($src, $width, $height)
 {
     // initializing
     $save_path = Wordless::theme_temp_path();
     $img_filename = Wordless::join_paths($save_path, md5($width . 'x' . $height . '_' . basename($src)) . '.jpg');
     // if file doesn't exists, create it
     if (!file_exists($img_filename)) {
         $to_scale = 0;
         $to_crop = 0;
         // Get orig dimensions
         list($width_orig, $height_orig, $type_orig) = getimagesize($src);
         // get original image ... to improve!
         switch ($type_orig) {
             case IMAGETYPE_JPEG:
                 $image = imagecreatefromjpeg($src);
                 break;
             case IMAGETYPE_PNG:
                 $image = imagecreatefrompng($src);
                 break;
             case IMAGETYPE_GIF:
                 $image = imagecreatefromgif($src);
                 break;
             default:
                 return;
         }
         // which is the new smallest?
         if ($width < $height) {
             $min_dim = $width;
         } else {
             $min_dim = $height;
         }
         // which is the orig smallest?
         if ($width_orig < $height_orig) {
             $min_orig = $width_orig;
         } else {
             $min_orig = $height_orig;
         }
         // image of the right size
         if ($height_orig == $height && $width_orig == $width) {
         } else {
             if ($width_orig < $width) {
                 $to_scale = 1;
                 $ratio = $width / $width_orig;
             } else {
                 if ($height_orig < $height) {
                     $to_scale = 1;
                     $ratio = $height / $height_orig;
                 } else {
                     if ($height_orig > $height && $width_orig > $width) {
                         $to_scale = 1;
                         $ratio_dest = $width / $height;
                         $ratio_orig = $width_orig / $height_orig;
                         if ($ratio_dest > $ratio_orig) {
                             $ratio = $width / $width_orig;
                         } else {
                             $ratio = $height / $height_orig;
                         }
                     } else {
                         if ($width == $width_orig && $height_orig > $height || $height == $height_orig && $width_orig > $width) {
                             $to_crop = 1;
                         } else {
                             echo "ALARM";
                         }
                     }
                 }
             }
         }
         // we need to zoom to get the right size
         if ($to_scale == 1) {
             $new_width = $width_orig * $ratio;
             $new_height = $height_orig * $ratio;
             $image_scaled = imagecreatetruecolor($new_width, $new_height);
             // scaling!
             imagecopyresampled($image_scaled, $image, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
             $image = $image_scaled;
             if ($new_width > $width || $new_height > $height) {
                 $to_crop = 1;
             }
         } else {
             $new_width = $width_orig;
             $new_height = $height_orig;
         }
         // we need to crop the image
         if ($to_crop == 1) {
             $image_cropped = imagecreatetruecolor($width, $height);
             // find margins for images
             $margin_x = ($new_width - $width) / 2;
             $margin_y = ($new_height - $height) / 2;
             // cropping!
             imagecopy($image_cropped, $image, 0, 0, $margin_x, $margin_y, $width, $height);
             $image = $image_cropped;
         }
         // Save image
         imagejpeg($image, $img_filename, 95);
     }
     // Return image URL
     return Wordless::join_paths(Wordless::theme_temp_path(), basename($img_filename));
 }