76948

Using regex replace with smarty template system?

Here's what I currently have in my Smarty template:

<div class="tbl_pagination"> {if $pager}{$pager->links}{/if} <div>

My {$pager->links} will output this HTML:

<div class="tbl_pagination"> <ul><li><a href="javascript:toPage(3)" title="previous page">Back</a></li> <li><a href="javascript:toPage(1)" title="page 1">1</a></li> <li><a href="javascript:toPage(2)" title="page 2">2</a></li> <li><a href="javascript:toPage(3)" title="page 3">3</a></li> <li class='active'>4</li> <li><a href="javascript:toPage(5)" title="page 5">5</a></li> <li><a href="javascript:toPage(6)" title="page 6">6</a></li> <li><a href="javascript:toPage(7)" title="page 7">7</a></li> <li><a href="javascript:toPage(5)" title="next page">Next</a></li> </ul></div> </div>

I'm trying to replace two different things:

<ol> <li>

I want to completed remove the title attribute.

</li> <li>

Whichever li has the class active needs to also have an a href.

</li> </ol>

Here's what I'd like it to look like:

<div class="tbl_pagination"> <ul><li><a href="javascript:toPage(3)">Back</a></li> <li><a href="javascript:toPage(1)">1</a></li> <li><a href="javascript:toPage(2)">2</a></li> <li><a href="javascript:toPage(3)">3</a></li> <li class='active'><a href="#">4</a></li> <li><a href="javascript:toPage(5)">5</a></li> <li><a href="javascript:toPage(6)">6</a></li> <li><a href="javascript:toPage(7)">7</a></li> <li><a href="javascript:toPage(5)">Next</a></li> </ul></div> </div>

Is this possible to do in the Smarty template system using their replace function?

Answer1:

Since you mentioned in your comments that you achieved (2) on your own, here's a solution for the first replace, with 2 methods:

<ol> <li>

You can use regex_replace:

{$pager->links|regex_replace:'/title="[\w\s]+"/':""}

</li> <li>

Alternatively, you can use directly php's preg_replace, like this:

{'/title="[\w\s]+"/'|preg_replace:'':$pager->links}

</li> </ol>

This might seem weird at first, but let me explain the syntax:

Smarty supports passing a variable/string to some php function using the | (pipe) symbol. However, additional parameters are then passed with the : (colon) parameter. This is consistent for the syntax of Variable Modifiers

For example, if you wanted to count the letters on your string, you would do:

{$pager->links|strlen}

And if you wanted to see if a value foo is within an array $bar you would do:

{'foo'|in_array:$bar}

so, for a php function that looks like func($arg1, $arg2, $arg3), this translates to {$arg1|func:$arg2:$arg3}

Needless to say the preferred method is (1), I only suggested the 2nd because, to my opinion, it is interesting to know.

Recommend

  • How to capture enclosing scope in Runnable
  • How to replace TouchesBegan with UIGestureRecognizer
  • keeping track of nested ng-repeat index's
  • Msvcr71.dll Msvcp71.dll missing
  • Asynchronous Sockets - Handling false socket.AcceptAsync values
  • Is is safe to use HSQLDB for production? (JBoss AS5.1)
  • Find longest path less than or equal to given value of an acyclic, directed graph in Python
  • Action Pack components in Rails
  • What is the correct way to synchronize a shared, static object in Java?
  • Fail:(TESTMODE) Transactions of this market type cannot be processed on this system
  • SAXReader not re-ecape characters
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Visual Studio 2010 debugger build correctly - compiler pdb and linker pdb not in synch?
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • Custom Tabgroup Appcelerator
  • iOS: Detect app start via notification press
  • Checking free space on FTP server
  • Initializer list vs. initialization method
  • Regex thinks I'm nesting, but I'm not
  • What is the “return” in scheme?
  • HTML download movie download link
  • Where to put my custom functions in Wordpress?
  • htaccess rewriting URLs with multiple forward slashes
  • Display Images one by one with next and previous functionality
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Web-crawler for facebook in python
  • R: gsub and capture
  • AT Commands to Send SMS not working in Windows 8.1
  • How to disable jQuery.jplayer autoplay?
  • A cron job substitute?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How does Linux kernel interrupt the application?
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?