0

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?

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
Jack
  • 9,615
  • 18
  • 72
  • 112
  • Do these five lines always occur in the same order? Or, in other words, how do I know which one of them contains the view you want to load? – Tim Pietzcker Apr 12 '10 at 11:15
  • They always occur in the same order. The fourth line always contains the view I want to load into the main content area. So the fourth line is the dynamic one, otherwise lines 1, 2, 3 and 5 always remain the same. – Jack Apr 12 '10 at 11:17

1 Answers1

1

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);
chris
  • 9,745
  • 1
  • 27
  • 27
  • Nice one! I'll test it on a sample PHP file first and report back shortly. – Jack Apr 12 '10 at 11:19
  • My bad, haven't had enough coffee this morning. Wasn't in Regex mode, I'll try again! :D – Jack Apr 12 '10 at 11:24
  • 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 – chris Apr 12 '10 at 11:27
  • 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 Apr 12 '10 at 11:29
  • 1
    And possibly use `\r\n` if you're on a Windows system. – Tim Pietzcker Apr 12 '10 at 11:29
  • 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. – chris Apr 12 '10 at 11:31
  • good stuff, glad i could help. regexp's are awesome for this type of code maintenance. – chris Apr 12 '10 at 11:33