Skip to content

Scripting (OrchardCore.Scripting)

脚本(OrchardCore.Scripting)

Purpose

目的

The scripting module provides an API allowing you to evaluate custom scripts in different languages.

脚本模块提供了一个API,允许您评估不同语言的自定义脚本。

Usage

用法

Executing some script

执行一些脚本

The main interface is IScriptingManager.

主界面是IScriptingManager


public interface IScriptingManager

 <font color=#0099ff size=4 face="黑体">公共接口IScriptingManager</font> 


{

 <font color=#0099ff size=4 face="黑体">{</font> 


    IScriptingEngine GetScriptingEngine(string prefix);

    object Evaluate(string directive);  

    IList<IGlobalMethodProvider> GlobalMethodProviders { get; }

}

 <font color=#0099ff size=4 face="黑体">}</font> 


To evaluate an expression using a scripting engine, you must know which ones are available in the system.

要使用脚本引擎评估表达式,您必须知道系统中可用的表达式。

For instance, a JavaScript one is available by default and its prefix is js.

例如,默认情况下可以使用JavaScript,其前缀为“js”。

To return the current date and time as a string we could do something like this:

要将当前日期和时间作为字符串返回,我们可以执行以下操作:


var scriptingManager = serviceProvider.GetService<IScriptingManager>();

 <font color=#0099ff size=4 face="黑体">var scriptingManager = serviceProvider.GetService <IScriptingManager>();</font> 


var date = scriptingManager.Evaluate("js: Date().toISOString()");

 <font color=#0099ff size=4 face="黑体">var date = scriptingManager.Evaluate(“js:Date()。toISOString()”);</font> 


The js: prefix is used to describe in which language the code is written. Any module can provide

js:前缀用于描述代码编写的语言。任何模块都可以提供

a new scripting engine by implementing the IScriptingEngine interface.

通过实现IScriptingEngine接口实现新的脚本引擎。

Customizing the scripting environment

自定义脚本环境

Any module can provide custom methods for scripts independently of the chosen language.

任何模块都可以独立于所选语言为脚本提供自定义方法。

For instance the Contents module provides a uuid() helper method that computes a unique content item identifier.

例如,Contents模块提供了一个uuid()帮助器方法,用于计算唯一的内容项标识符。

To create a global method implement IGlobalMethodProvider then add it to the current IScriptingManager

要创建一个全局方法实现IGlobalMethodProvider,然后将其添加到当前的IScriptingManager

instance like this:

像这样的实例:


var scriptingManager = serviceProvider.GetService<IScriptingManager>();

 <font color=#0099ff size=4 face="黑体">var scriptingManager = serviceProvider.GetService <IScriptingManager>();</font> 


var globalMethodProvider = new MyGlobalMethodProvider();

 <font color=#0099ff size=4 face="黑体">var globalMethodProvider = new MyGlobalMethodProvider();</font> 


scriptingManager.GlobalMethodProviders.Add(globalMethodProvider);

 <font color=#0099ff size=4 face="黑体">scriptingManager.GlobalMethodProviders.Add(globalMethodProvider);</font> 


File

文件

The File scripting engine provides methods to read file contents.

文件脚本引擎提供了读取文件内容的方法。

| Name | Example | Description |

|名称|示例|说明|

| ---- | ---- | -------- |

| ---- | ---- | -------- |

| text | file:raw('../wwwroot/template.html') | Returns the content of a text file. |

| text | file:raw('../ wwwroot / template.html')|返回文本文件的内容。 |

| base64 | file:base64('../wwwroot/image.jpg') | Returns the base64 encoded content of a file. |

| base64 | file:base64('../ wwwroot / image.jpg')|返回文件的base64编码内容。 |