使用 context=php_new_file_context 为 Eclipse PDT 编写代码模板
我有一个基础课程,我经常扩展它。我认为在创建新文件时添加扩展模板作为选择会很好。我进入 Eclipse->Preferences->PHP->Code Style->Code Templates 并决定复制并修改“简单的 php 文件”。所以我导出该模板并打开它。它看起来像这样:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<templates>
<template autoinsert="true" context="php_new_file_context"
deleted="false" description="Simple php file" enabled="true"
id="org.eclipse.php.ui.editor.templates.php.author" name="New simple PHP file">
<?php
${cursor}
?>
</template>
</templates>
我编辑了文件,看起来像这样:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<templates>
<template autoinsert="true" context="php_new_file_context"
deleted="false" description="PHP Item subclass file" enabled="true"
id="org.eclipse.php.ui.editor.templates.php.itemclass" name="Item subclass file">
<?php
/**
* ${enter description}
* @author: My Name
* @version:
**/
class ${classname} extends Item {
const PKEY='${pkey}'; //name of primary key variable
const TABLE='${table}'; //name of db table
//db table vars
$${pkey};
$${name};
//history vars
public static function who() {
return __CLASS__;
}
}
?>
</template>
</templates>
然后我将其保存为 item.xml 并尝试导入它。导入没有出现错误,但新模板没有显示在列表中。我唯一能想到的是我为 id 属性选择的字符串有问题。但我找不到任何有关正确创作模板的参考资料。要么我的 google-fu 失败了,要么我正在尝试做一些我不应该做的事情;我不知道。
有想法吗?
I have a basic class that I extend fairly often. I thought it would be nice to add a template of the extension as a choice when creating a new file. I went to Eclipse->Preferences->PHP->Code Style->Code Templates and decided to copy and modify the "Simple php file". So I exported that template and opened it up. It looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<templates>
<template autoinsert="true" context="php_new_file_context"
deleted="false" description="Simple php file" enabled="true"
id="org.eclipse.php.ui.editor.templates.php.author" name="New simple PHP file">
<?php
${cursor}
?>
</template>
</templates>
I edited the file to look like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<templates>
<template autoinsert="true" context="php_new_file_context"
deleted="false" description="PHP Item subclass file" enabled="true"
id="org.eclipse.php.ui.editor.templates.php.itemclass" name="Item subclass file">
<?php
/**
* ${enter description}
* @author: My Name
* @version:
**/
class ${classname} extends Item {
const PKEY='${pkey}'; //name of primary key variable
const TABLE='${table}'; //name of db table
//db table vars
${pkey};
${name};
//history vars
public static function who() {
return __CLASS__;
}
}
?>
</template>
</templates>
Then I saved it as item.xml and tried to import it. The import threw no errors, but the new template didn't show up in the list. The only thing I can think is that the string I chose for the id attribute is problematic. But I can't find any references on properly authoring a template. Either my google-fu is failing, or I'm trying to do something I'm not supposed to; I don't know.
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是模板第三行的变量名称中有一个空格
${enter description}
,这是 Eclipse 不允许的,但这些变量不会不考虑自动填充。尽管如此,它不会让您导入它们,因为它是不再支持的功能,如 在他们的 bugtracker 中声明。您可以使用当前的模板系统,它可以让您添加变量以自动填充。要使用它,您必须转到
PHP >;编辑>模板
。然后,您可以使用您想要的任何名称创建一个新模板,当您开始在新的 php 文件中键入它时,如果您从自动完成中选择它。The problem you have here is that there is an space in the name of a variable the third line of the template,
${enter description}
and this isn't allowed by eclipse, but those variables won't be considered for autofilling.Even though, it'll not let you import them, because it's a feature that is no longer supported, as stated in their bugtracker. You could use the current templating system, wich will let you add variables to autofill. To use it, you have to go to
PHP > Editor > Templates
. Then, you'd create a new template with wichever name you want, and when you started typing it on a new php file, if you'd select it from the auto-complete.