Dreamweaver + Smarty

背景:
PHPのSmartyを使っていて不便なことは,Smartyのinclude命令によって外部ファイルを読み込んでいる場合である.そのテンプレートファイルをDreamweaverで編集するときにはSmartyタグがHTMLとして認識されるので非常に不便.また,ファイルがincludeされないので,include後の画面表示ができない.

ソリューション:
このページに載っている方法を使うと,HTMLファイルをDreamweaverで開くときにファイルをhookして,includeされたファイルを画面に表示してくれる.
http://smarty.incutio.com/?page=SmartyDreamweaver

手順:
1.以下のソースコードをファイルに保存し,”Dreamweaverをインストールしたディレクトリ\Configuration\Translators\”.に保存する.ファイル名はなんでもいい.

<html>
<head>
<title>Smarty Include Tag Translator</title>
<meta http-equiv="Content-Type" content="text/html; charset="/>
<script language="JavaScript">

/**
 * This translator will only work on this format:
 * {include file="foo.tpl"}
 * filename must surrounded by double or single quotes
 * if 'assign' or '[var]' attribute exists, it will not tranlated
 * the include tag must be in one line
 * extra spaces are allowed, e.g. { include file = " foo.tpl " }
 * all templates file suppose to stay in the same top 'templates' folder
 * you can include subfolder in the include tag
 * all other formats will be displayed as a simple 'inc' icon
 */

/**
 * define smarty delimiters
*/

var LDELIM = "{";
var RDELIM = "}";


function getTranslatorInfo()
{
	var transArray = new Array(6);
	transArray[0] = "SMARTY_INCLUDE";
	transArray[1] = "Smarty Include Translator";
	transArray[2] = "0";
	transArray[3] = "1";
	transArray[4] = LDELIM + "[ \t]*include";
	transArray[5] = "byExpression";
	return transArray; 
}

function translateMarkup( docNameStr, siteRootStr, inStr )
{
	var pos = 0;
	var patternFound = false;
	var outStr = '';
	var depPath = '';
	var remainInStr = inStr;
	smartyRegExp = new RegExp("(" + LDELIM + "[ \t]*include[ \t]+file[ \t]*=[ \t]*[\"|'][ \t]*([^\"^']*)[ \t]*[\"|'][ \t]*" + RDELIM + ")", "im");
	while ((pos = remainInStr.search(smartyRegExp)) >= 0)
	{
		var matchStr = RegExp.$1;
		var templateFileName = RegExp.$2;
		var templateFullName = templateFileName;
		var smartyFile;
		outStr += remainInStr.substr(0, pos);
		smartyFile = new File(templateFullName, docNameStr);
		if (smartyFile.exists())
		{
			smartyContent = smartyFile.getContents();
			if (smartyContent.length < = 0)
			{
				// this is an empty file
				smartyContent = " ";
			}
			depPath = smartyFile.getAbsolutePath();
		}
		else
		{
			// file doesn't exist
			smartyContent = "File Error!";
		}
		
		// Do translation
		outStr += '<MM:BeginLock translatorClass="' + getTranslatorInfo()[0] + '" type="smartyInclude" orig="' + escape(matchStr) + '"';
		if (depPath.length > 0)
			outStr += ' depFiles="' + depPath + '"';
		outStr += '>' + smartyContent + '<mm:endlock>';
		
		// Re-search text following match
		remainInStr = remainInStr.substring(pos + matchStr.length);
		// Remember that at least one translation was performed
		patternFound = true;
	}
	outStr += remainInStr;
	return patternFound ? outStr : "";
}
</mm:endlock></script>
</head>
<body>
</body>
</html>

2.
Dreamweaverを再起動する.

たったこれだけで,格段に作業が楽になる.


matsubokkuri

Please feel free to contact me via e-mail, twitter and facebook!

あわせて読みたい

コメントを残す