FROM: http://www.sdtclass.com/4799.html
15年的時候,我寫過一篇文章《略談 vqmod for opencart 插件制作過程》,也不知道被哪些人抄襲過去了。不過無所謂了。文章梳理的思路還是在我這里。今天對這篇文章進行進一步補充。
file標簽
如果有多個類似文件,我們寫很多個嗎?其實可以不必要,利用path和逗號解決這個問題。
- <!-- 修改最新產品模塊 -->
- <file name="catalog/view/theme/*/template/module/latest.tpl">
- <!-- 規則 -->
- </file>
- <!-- 修改熱賣產品模塊 -->
- <file name="catalog/view/theme/*/template/module/bestseller.tpl">
- <!-- 規則 -->
- </file>
- <!-- 改為 -->
- <file path="catalog/view/theme/*/template/module/" name="latest.tpl,bestseller.tpl">
- <!-- 規則 -->
- </file>
其中,上面的 * 代表任何文件,如果有多個模板可以這么解決。
operation標簽
如果一個地方,搜索不到,導致整個XML不起作用怎么辦呢?可以定義跳過,不過不要總使用這樣的,比如你TPL搜索到了代碼,但是C層或者M層搜索不到,那樣會導致頁面報錯。所以C層和M層不建議使用跳過,而V層可以。
- <file name="catalog/view/theme/*/template/product/product.tpl">
- <operation error="skip">
- <!-- 規則 -->
- </operation>
- </file>
當然了,如果你要寫個注釋,比如這個功能干嘛的。你可以這樣寫:
- <file name="catalog/view/theme/*/template/product/product.tpl">
- <operation error="skip" info="添加xx功能">
- <!-- 規則 -->
- </operation>
- </file>
regex
如果你想用正則搜索怎么辦呢?有時候普通搜索很難定位,其實vqmod也是支持的啦。
- <file name="catalog/view/theme/*/template/product/product.tpl">
- <operation error="skip" info="添加xx功能">
- <search position="replace" regex="true"><![CDATA[~<div(.*?)class="(.*?)image(.*?)"(.*?)>~]]></search>
- <!-- 規則 -->
- </operation>
- </file>
正則方面以后會寫一期文章,如果你不熟悉,可以用別的替代方法,比如上面提到的文章里提到的index,也可以用下面這個。
offset
這個是用於搜索到的第幾行,用於position的參數是after或者before的時候。可以是正數或者負數。如果是after,搜索到的行,往下數一行然后添加代碼,就是1。如果你是before,然后往上數兩行再添加代碼在上面,就是2。
下面搜索這段代碼添加作為例子。
- <div class="col-sm-12">
- <label class="control-label"><?php echo $entry_rating; ?></label>
- <?php echo $entry_bad; ?>
- <input type="radio" name="rating" value="1" />
-
- <input type="radio" name="rating" value="2" />
-
- <input type="radio" name="rating" value="3" />
-
- <input type="radio" name="rating" value="4" />
-
- <input type="radio" name="rating" value="5" />
- <?php echo $entry_good; ?></div>
- </div>
用XML對這段代碼進行添加代碼。如下四個示例:
- <file name="catalog/view/theme/*/template/product/product.tpl" keep="true">
- <operation error="skip">
- <search position="after" offset="1"><![CDATA[<input type="radio" name="rating" value="1" />]]></search>
- <add><![CDATA[<b>測試1</b>]]></add>
- </operation>
- <operation error="skip">
- <search position="after" offset="-1"><![CDATA[<input type="radio" name="rating" value="2" />]]></search>
- <add><![CDATA[<b>測試2</b>]]></add>
- </operation>
- <operation error="skip">
- <search position="before" offset="1"><![CDATA[<input type="radio" name="rating" value="3" />]]></search>
- <add><![CDATA[<b>測試4</b>]]></add>
- </operation>
- <operation error="skip">
- <search position="before" offset="-1"><![CDATA[<input type="radio" name="rating" value="4" />]]></search>
- <add><![CDATA[<b>測試4</b>]]></add>
- </operation>
- </file>
你會得到下面的緩存代碼:
大家從上面的圖片中不難發現,before的時候offset是負數1的時候和直接用after,不用offset沒區別,after的時候offset是負數1的時候和直接用before不用offset的也沒區別,負數值大的時候,offset是-5,position是after等同於offset=4,position是before。所以其實offset有負數功能,但是也沒太大意義。
iafter和ibefore
相比於after和before,前面加個i的區別就是,在搜索到的代碼后面或者前面添加。下面給個例子:
【別問我為啥顏色不一樣,我有幾個編輯器。】
當sql這里要添加代碼的時候,比如 telephone = '" . $this->db->escape($data['telephone']) . "',
代碼:
- <operation info="添加手機號">
- <search position="iafter"><![CDATA[city = '" . $this->db->escape($data['city']) . "',]]></search>
- <add><![CDATA[ telephone = '" . $this->db->escape($data['telephone']) . "',]]></add>
- </operation>
這樣的話,就會在搜索到的代碼后面,加入add里指定的代碼。值得注意的是,平時使用其他的position時,add定義的你怎么換行都沒問題,如果是iafter、ibefore和replace的時候,代碼的開頭最好緊挨着<add><![CDATA[ 寫,為啥這樣呢,因為這樣SQL又是一整行,而且有時候不適合換行。也可能對后面別人開發的插件代碼干擾。以后講講vqmod的兼容問題。
ibefore這里不舉例子了,同理的。搜索的前面加,和搜索的后面加的效果。
輸出:
- //iafter 的話
- city = '" . $this->db->escape($data['city']) . "', telephone = '" . $this->db->escape($data['telephone']) . "',
- //ibefore的話
- telephone = '" . $this->db->escape($data['telephone']) . "',city = '" . $this->db->escape($data['city']) . "',
PS:最近太忙了,更新頻率自然下降很多了。以后多抽空更新。歡迎關注我們公眾號“SDT技術網”。