vqmod for opencart插件制作進階與技巧


FROM: http://www.sdtclass.com/4799.html

15年的時候,我寫過一篇文章《略談 vqmod for opencart 插件制作過程》,也不知道被哪些人抄襲過去了。不過無所謂了。文章梳理的思路還是在我這里。今天對這篇文章進行進一步補充。

file標簽

如果有多個類似文件,我們寫很多個嗎?其實可以不必要,利用path和逗號解決這個問題。

 
  1. <!-- 修改最新產品模塊 -->
  2. <file name="catalog/view/theme/*/template/module/latest.tpl">
  3. <!-- 規則 -->
  4. </file>
  5. <!-- 修改熱賣產品模塊 -->
  6. <file name="catalog/view/theme/*/template/module/bestseller.tpl">
  7. <!-- 規則 -->
  8. </file>
  9. <!-- 改為 -->
  10. <file path="catalog/view/theme/*/template/module/" name="latest.tpl,bestseller.tpl">
  11. <!-- 規則 -->
  12. </file>

其中,上面的 * 代表任何文件,如果有多個模板可以這么解決。

operation標簽

如果一個地方,搜索不到,導致整個XML不起作用怎么辦呢?可以定義跳過,不過不要總使用這樣的,比如你TPL搜索到了代碼,但是C層或者M層搜索不到,那樣會導致頁面報錯。所以C層和M層不建議使用跳過,而V層可以。

 
  1. <file name="catalog/view/theme/*/template/product/product.tpl">
  2.     <operation error="skip">
  3.         <!-- 規則 -->
  4.     </operation>
  5. </file>

當然了,如果你要寫個注釋,比如這個功能干嘛的。你可以這樣寫:

 
  1. <file name="catalog/view/theme/*/template/product/product.tpl">
  2.     <operation error="skip" info="添加xx功能">
  3.         <!-- 規則 -->
  4.     </operation>
  5. </file>

regex

如果你想用正則搜索怎么辦呢?有時候普通搜索很難定位,其實vqmod也是支持的啦。

 
  1. <file name="catalog/view/theme/*/template/product/product.tpl">
  2.     <operation error="skip" info="添加xx功能">
  3.         <search position="replace" regex="true"><![CDATA[~<div(.*?)class="(.*?)image(.*?)"(.*?)>~]]></search>
  4.         <!-- 規則 -->
  5.     </operation>
  6. </file>

正則方面以后會寫一期文章,如果你不熟悉,可以用別的替代方法,比如上面提到的文章里提到的index,也可以用下面這個。

offset

這個是用於搜索到的第幾行,用於position的參數是after或者before的時候。可以是正數或者負數。如果是after,搜索到的行,往下數一行然后添加代碼,就是1。如果你是before,然后往上數兩行再添加代碼在上面,就是2。

下面搜索這段代碼添加作為例子。

 
  1. <div class="col-sm-12">
  2.     <label class="control-label"><?php echo $entry_rating; ?></label>
  3.     &nbsp;&nbsp;&nbsp; <?php echo $entry_bad; ?>&nbsp;
  4.     <input type="radio" name="rating" value="1" />
  5.     &nbsp;
  6.     <input type="radio" name="rating" value="2" />
  7.     &nbsp;
  8.     <input type="radio" name="rating" value="3" />
  9.     &nbsp;
  10.     <input type="radio" name="rating" value="4" />
  11.     &nbsp;
  12.     <input type="radio" name="rating" value="5" />
  13.      &nbsp;<?php echo $entry_good; ?></div>
  14. </div>

用XML對這段代碼進行添加代碼。如下四個示例:

 
  1. <file name="catalog/view/theme/*/template/product/product.tpl" keep="true">
  2.     <operation error="skip">
  3.         <search position="after" offset="1"><![CDATA[<input type="radio" name="rating" value="1" />]]></search>
  4.         <add><![CDATA[<b>測試1</b>]]></add>
  5.     </operation>
  6.     <operation error="skip">
  7.         <search position="after" offset="-1"><![CDATA[<input type="radio" name="rating" value="2" />]]></search>
  8.         <add><![CDATA[<b>測試2</b>]]></add>
  9.     </operation>
  10.     <operation error="skip">
  11.         <search position="before" offset="1"><![CDATA[<input type="radio" name="rating" value="3" />]]></search>
  12.         <add><![CDATA[<b>測試4</b>]]></add>
  13.     </operation>
  14.     <operation error="skip">
  15.         <search position="before" offset="-1"><![CDATA[<input type="radio" name="rating" value="4" />]]></search>
  16.         <add><![CDATA[<b>測試4</b>]]></add>
  17.     </operation>
  18. </file>

你會得到下面的緩存代碼:

vqmod for opencart插件制作進階與技巧

大家從上面的圖片中不難發現,before的時候offset是負數1的時候和直接用after,不用offset沒區別,after的時候offset是負數1的時候和直接用before不用offset的也沒區別,負數值大的時候,offset是-5,position是after等同於offset=4,position是before。所以其實offset有負數功能,但是也沒太大意義。

iafter和ibefore

相比於after和before,前面加個i的區別就是,在搜索到的代碼后面或者前面添加。下面給個例子:

vqmod for opencart插件制作進階與技巧

【別問我為啥顏色不一樣,我有幾個編輯器。】

當sql這里要添加代碼的時候,比如 telephone = '" . $this->db->escape($data['telephone']) . "',

代碼:

 
  1. <operation info="添加手機號">
  2.     <search position="iafter"><![CDATA[city = '" . $this->db->escape($data['city']) . "',]]></search>
  3.     <add><![CDATA[ telephone = '" . $this->db->escape($data['telephone']) . "',]]></add>
  4. </operation>

這樣的話,就會在搜索到的代碼后面,加入add里指定的代碼。值得注意的是,平時使用其他的position時,add定義的你怎么換行都沒問題,如果是iafter、ibefore和replace的時候,代碼的開頭最好緊挨着<add><![CDATA[ 寫,為啥這樣呢,因為這樣SQL又是一整行,而且有時候不適合換行。也可能對后面別人開發的插件代碼干擾。以后講講vqmod的兼容問題。

ibefore這里不舉例子了,同理的。搜索的前面加,和搜索的后面加的效果。

輸出:

 
  1. //iafter 的話
  2. city = '" . $this->db->escape($data['city']) . "', telephone = '" . $this->db->escape($data['telephone']) . "',
  3. //ibefore的話
  4.  telephone = '" . $this->db->escape($data['telephone']) . "',city = '" . $this->db->escape($data['city']) . "',

PS:最近太忙了,更新頻率自然下降很多了。以后多抽空更新。歡迎關注我們公眾號“SDT技術網”。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM