sábado, febrero 18, 2012

Codeigniter: Working with template views

Codeigniter doesn't provide a template engine, and it makes that each views has all html code.
Including files is a good idea, but I'm not happy adding every time de include code, is not DRY.

Well the best way to work with templates is the Decorator Design Pattern, and for make it possible in Codeigniter I was used the Most Simple Template Library for CodeIgniter until that discovered the next: Using another view like template.

Using it is very easy, is not neccesary use other library and you can add any functionality.

How to make it.

Simple use

Controller
public function action(){
   $data = array(
     'view' => 'view_name',
     'title' => 'Hello World'
   );
   $this->load->view('template_tpl', $data);
}

View
php  + view html

Template view
php + general html
$this->load->view($view);
php + general html

File system
/application/views/template_tpl.php
/application/views/view_name.php


Advanced use

If you use different sections in your site/application the simple use is not ver helpful. Following the same philosophy you can make possible it.

Controller
public function action(){
   $data = array(
     'section1' => 'section1',
     'section2' => 'section2',
     'view' => 'view_name',
     'title' => 'Hello World'
   );
   $this->load->view('template_tpl', $data);
}

View
php  + view html

Section
php  + section html

Template view
php + general html
section($section1);
php + general html
$this->load->view($view);
section($section2);
php + general html

File system
/application/views/template_tpl.php
/application/views/view_name.php
/application/views/section_name.php


Solve a little problem

To work with sections, we need to solve a little problem: If you don't send a section name to template, codeigniter show the next error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: variable_nameFilename: views/file.php
Line Number: x
To solve it we need to add this function to our template:
function section(&$name){
    $CI = & get_instance();
    isset($name) ? $CI->load->view($name) : null;
}




martes, febrero 07, 2012

Codeigniter: Language lines into javascript


I’ve extend language_helper and I’ve added a langJs function.

For use it you only call it with an array including lines for pass to javascript.

Example

In the controller
$this->load->helper('language');
$this->lang->load('calendar', 'spanish');

In the view
[php]

echo langJs(array('cal_wednesday', 'cal_january'));

[javascript]
alert(_msg.cal_wednesday);

Load all lines
If you need load all lines, you can do something like this:

In the Controller

$this->load->helper('language');
$this->lang->load('calendar', 'spanish');
$data['lang_keys'] = array_keys($this->lang->language);

In the View
echo langJs($lang_keys);

As you can see, is very easy passing messages to javascript.
Download Language Helper

jueves, diciembre 22, 2011

CodeIgniter: link_tag improved

This is a custom html_helper to improve use of link_tag. Also I've added js_tag to make the same result.


For this I’ve created ‘MY_html_helper’ custom helper, so you can use easily.
Only copy file into ‘helpers’ in your project.

The functions:

[php]

link_tag();
js_tag();


Example

[php]

// Change css/main.css -> css/main.css?{mtime} -> css/main.css?12342343 automaticaly.
echo link_tag(‘css/main.css’);
echo js_tag(‘js/system.js’);



Download link_tag improved

domingo, diciembre 11, 2011

Krusader Filemanager

Simply krusader is the best file manager that I've tried.
In my work I need use a file manager like total commander for linux, but the most aren't fully useful.

Always I used Filezilla because it has the features that I needed, but now I need:
  • Sync-browse mode
  • Compare directories and files
  • Manage Samba, FTP, local files
  • Manage FTP accounts
  • Filter files quickly
  • Add user commands
  • Manage tabs
  • keyboard usable
  • Advanced configuration
And Krusader achieve these requirements.

Some problems and solutions

I've had two problems using krusader.

Doesn't save FTP passwords

Solution
  1. Install KDE Wallet
  2. Create new Wallet
Is not possible manage bookmarks

Solution

martes, noviembre 01, 2011

PHP [NO-IF]: Make option selected

Technique for make option selected without IF statement.

[php]
//came from anywhere: post, get, cookie, etc.
$color_selected = 'red';

//Required if works in a strict environment
$colors = array(
  'blue' => null,
  'red' =>  null,
  'white' => null,
);

//Only set selected color
$colors[$color_selected] = 'selected="selected"';

echo <<<PQR
What Color?
<select name="color">
    <option value="blue" {$colors['blue']}>Blue</option>
    <option value="red" {$colors['red']}>Red</option>
    <option value="white" {$colors['white']}>White</option>
</select>
PQR;

[/php]

sábado, octubre 01, 2011

Php MongoDB Phonebook Example

This is a example to explain how to use MongoDb with Php. In this post I will explain how to download and start MongoDb. Next you can run the Phonebook aplication.

Download MongoDb

http://www.mongodb.org/downloads

For this example I tested 2.0.0 version for linux

Start MongoDb

Extract the file anywhere, and in mongodb-x-x-x directory create 'data' directory. Next run:

[terminal]
$ ./bin/mongod --dbpath data

* 'data' is the name of directory for save data

Run Phonebook example

Download: http://www.mediafire.com/?mqncj9lsjrts3jx

And extract to /var/www/ the application

Next go to http://localhost/MongoPhoneBook/

Enjoy!

lunes, septiembre 26, 2011

Acortador de url con php, hash y nosql

Hace unas semanas cree a modo de experimento esta simple aplicación para acortar urls.
Su uso es muy simple, está en modo de desarrollador. Así que si deseas una interfaz, deberás crearla.
Pero la parte "difícil" está hecha.

Vi varios sistemas, pero quería uno que no usara base de datos, por la rapidez. Tampoco quería duplicar datos, ni menos validar. Es por esto que utilicé Adler32 para disminuir el riesgo de colisiones.

El siguiente paso sería disminuir el hash de 8 a 6 ó 5 carácteres.

Si quieres ver el código y descargarlo:

https://sourceforge.net/projects/url-shortener/