views:

112

answers:

1

Hey folks.

Regexs make me cry, so, I came here for help.

I'm looking for some tips on Find & Replace in Panic's Coda. I know the F&R is pretty advance but I'm just looking for the best way to do this. I'm trying to rewrite a 'template engine' (very basic) I have going on with a webapp I'm coding in PHP (CodeIgniter).

Currently I'm calling my template like so:

$this->load->view('subviews/template/headerview');
$this->load->view('subviews/template/menuview');
$this->load->view('subviews/template/sidebar');
$this->load->view('The-View-I-Want-To-Load');   // This is the important line
$this->load->view('subviews/template/footerview');

However it's inefficient using five lines of code every time I want to load up a different page, in every different controller. So I rewrote it like this:

$data['view'] = 'The-View-I-Want-To-Load';
$this->load->view('template',$data);

That way if I need to make any major changes to the design it can easily be done from the template.php view file (which contains the header, menu, sidebar views etc. etc.).

However I use the previous 5-lines all over the place in many different controllers and functions. So, my question is --- How can I find and replace the old template engine (5 lines of code) for the new one - substituting in the name of the view in the important, unique line for the one in $data['view]?

Does that make any sense?! If not I'll try and rephrase! I mean, is there a way of doing this via a Regex or something? Or am I on completely the wrong lines here?

Thanks for your help

Jack

+1  A: 

your regex will look something like this :

\$this->load->view\('subviews/template/headerview'\);\n\$this->load->view\('subviews/template/menuview'\);\n\$this->load->view\('subviews/template/sidebar'\);\n\$this->load->view\('([^']*)'\);\n\$this->load->view\('subviews/template/footerview'\);

and replace with

\$data['view'] = '$1';\n\$this->load->view('template',\$data);
oedo
Nice one! I'll test it on a sample PHP file first and report back shortly.
Jack Webb-Heller
Not having much luck. The finding regex never finds anything...
Jack Webb-Heller
My bad, haven't had enough coffee this morning. Wasn't in Regex mode, I'll try again! :D
Jack Webb-Heller
depending on the software, you might need to double-escape the \ chars. i think i got them all escaped properly, but it's still the morning for me too :P
oedo
Haha, no worries :) OK, in Coda, this still doesn't find anything with Regex turned on. I have several options: Syntax (Java, Perl, Ruby, GNU, grep, POSIX, POSIX Basic, etc.) - it's set to Ruby by default. Escape Character is set to \. What's next?! I'm pretty confused here...
Jack Webb-Heller
And possibly use `\r\n` if you're on a Windows system.
Tim Pietzcker
Just tried double escaping everything too. Still no luck.
Jack Webb-Heller
are there any tabs/spaces? you could replace the \n in the search string with \n\s* instead, which will replace any number of whitespaces after the linebreak.
oedo
Nope, I'm on a Mac...
Jack Webb-Heller
YAY! That works with \n\s*! Thank you very much! :D
Jack Webb-Heller
good stuff, glad i could help. regexp's are awesome for this type of code maintenance.
oedo