首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

text/template/parse

  • import "text/template/parse"
  • 概观
  • 索引

概观

解析构包建为 text / template和html / template 定义的模板分析树。客户应该使用这些软件包来构建模板,而不是这个软件包,它提供了不用于一般用途的共享内部数据结构。

索引

  • func IsEmptyTree(n Node) bool
  • func Parse(name, text, leftDelim, rightDelim string, funcs ...map[string]interface{}) (map[string]*Tree, error)
  • type ActionNode
  • func (a *ActionNode) Copy() Node
  • func (a *ActionNode) String() string
  • type BoolNode
  • func (b *BoolNode) Copy() Node
  • func (b *BoolNode) String() string
  • type BranchNode
  • func (b *BranchNode) Copy() Node
  • func (b *BranchNode) String() string
  • type ChainNode
  • func (c *ChainNode) Add(field string)
  • func (c *ChainNode) Copy() Node
  • func (c *ChainNode) String() string
  • type CommandNode
  • func (c *CommandNode) Copy() Node
  • func (c *CommandNode) String() string
  • type DotNode
  • func (d *DotNode) Copy() Node
  • func (d *DotNode) String() string
  • func (d *DotNode) Type() NodeType
  • type FieldNode
  • func (f *FieldNode) Copy() Node
  • func (f *FieldNode) String() string
  • type IdentifierNode
  • func NewIdentifier(ident string) *IdentifierNode
  • func (i *IdentifierNode) Copy() Node
  • func (i *IdentifierNode) SetPos(pos Pos) *IdentifierNode
  • func (i *IdentifierNode) SetTree(t *Tree) *IdentifierNode
  • func (i *IdentifierNode) String() string
  • type IfNode
  • func (i *IfNode) Copy() Node
  • type ListNode
  • func (l *ListNode) Copy() Node
  • func (l *ListNode) CopyList() *ListNode
  • func (l *ListNode) String() string
  • type NilNode
  • func (n *NilNode) Copy() Node
  • func (n *NilNode) String() string
  • func (n *NilNode) Type() NodeType
  • type Node
  • type NodeType
  • func (t NodeType) Type() NodeType
  • type NumberNode
  • func (n *NumberNode) Copy() Node
  • func (n *NumberNode) String() string
  • type PipeNode
  • func (p *PipeNode) Copy() Node
  • func (p *PipeNode) CopyPipe() *PipeNode
  • func (p *PipeNode) String() string
  • type Pos
  • func (p Pos) Position() Pos
  • type RangeNode
  • func (r *RangeNode) Copy() Node
  • type StringNode
  • func (s *StringNode) Copy() Node
  • func (s *StringNode) String() string
  • type TemplateNode
  • func (t *TemplateNode) Copy() Node
  • func (t *TemplateNode) String() string
  • type TextNode
  • func (t *TextNode) Copy() Node
  • func (t *TextNode) String() string
  • type Tree
  • func New(name string, funcs ...map[string]interface{}) *Tree
  • func (t *Tree) Copy() *Tree
  • func (t *Tree) ErrorContext(n Node) (location, context string)
  • func (t *Tree) Parse(text, leftDelim, rightDelim string, treeSet map[string]*Tree, funcs ...map[string]interface{}) (tree *Tree, err error)
  • type VariableNode
  • func (v *VariableNode) Copy() Node
  • func (v *VariableNode) String() string
  • type WithNode
  • func (w *WithNode) Copy() Node

文件包

func IsEmptyTreeSource

代码语言:javascript
复制
func IsEmptyTree(n Node) bool

IsEmptyTree 报告此树(节点)是否为空而不是空间。

func ParseSource

代码语言:javascript
复制
func Parse(name, text, leftDelim, rightDelim string, funcs ...map[string]interface{}) (map[string]*Tree, error)

Parse 从模板名称返回映射到 parse.Tree,通过解析参数字符串中描述的模板创建。顶层模板将被赋予指定的名称。如果遇到错误,解析将停止并返回一个空映射并显示错误。

键入ActionNodeSource

ActionNode 包含一个动作(由分隔符限定)。控制动作有自己的节点;ActionNode 表示简单的例如字段评估和圆括号的管道。

代码语言:javascript
复制
type ActionNode struct {
        NodeType
        Pos

        Line int       // The line number in the input. Deprecated: Kept for compatibility.
        Pipe *PipeNode // The pipeline in the action.
        // contains filtered or unexported fields
}

func (*ActionNode) CopySource

代码语言:javascript
复制
func (a *ActionNode) Copy() Node

func (*ActionNode) StringSource

代码语言:javascript
复制
func (a *ActionNode) String() string

键入BoolNodeSource

BoolNode 保存一个布尔常量。

代码语言:javascript
复制
type BoolNode struct {
        NodeType
        Pos

        True bool // The value of the boolean constant.
        // contains filtered or unexported fields
}

func (*BoolNode) CopySource

代码语言:javascript
复制
func (b *BoolNode) Copy() Node

func (*BoolNode) StringSource

代码语言:javascript
复制
func (b *BoolNode) String() string

键入BranchNodeSource

BranchNode 是 if,range 和 with 的常见表示。

代码语言:javascript
复制
type BranchNode struct {
        NodeType
        Pos

        Line     int       // The line number in the input. Deprecated: Kept for compatibility.
        Pipe     *PipeNode // The pipeline to be evaluated.
        List     *ListNode // What to execute if the value is non-empty.
        ElseList *ListNode // What to execute if the value is empty (nil if absent).
        // contains filtered or unexported fields
}

func (*BranchNode) CopySource

代码语言:javascript
复制
func (b *BranchNode) Copy() Node

func (*BranchNode) StringSource

代码语言:javascript
复制
func (b *BranchNode) String() string

type ChainNodeSource

ChainNode 包含一个术语,后跟一串字段访问(标识符以'。'开头)。这些名字可能被链接('.x.y')。期间从每个身份证中删除。

代码语言:javascript
复制
type ChainNode struct {
        NodeType
        Pos

        Node  Node
        Field []string // The identifiers in lexical order.
        // contains filtered or unexported fields
}

func (*ChainNode) AddSource

代码语言:javascript
复制
func (c *ChainNode) Add(field string)

添加将指定的字段(应以句点开头)添加到链的末尾。

func (*ChainNode) CopySource

代码语言:javascript
复制
func (c *ChainNode) Copy() Node

func (*ChainNode) StringSource

代码语言:javascript
复制
func (c *ChainNode) String() string

键入CommandNodeSource

CommandNode 包含一个命令(一个评估操作中的管道)。

代码语言:javascript
复制
type CommandNode struct {
        NodeType
        Pos

        Args []Node // Arguments in lexical order: Identifier, field, or constant.
        // contains filtered or unexported fields
}

func (*CommandNode) CopySource

代码语言:javascript
复制
func (c *CommandNode) Copy() Node

func (*CommandNode) StringSource

代码语言:javascript
复制
func (c *CommandNode) String() string

type DotNodeSource

DotNode 包含特殊标识符'。'。

代码语言:javascript
复制
type DotNode struct {
        NodeType
        Pos
        // contains filtered or unexported fields
}

func (*DotNode) CopySource

代码语言:javascript
复制
func (d *DotNode) Copy() Node

func (*DotNode) StringSource

代码语言:javascript
复制
func (d *DotNode) String() string

func (*DotNode) TypeSource

代码语言:javascript
复制
func (d *DotNode) Type() NodeType

type FieldNodeSource

FieldNode 包含一个字段(标识符以'。'开始)。这些名字可能被链接('.x.y')。该期限从每个身份证件中删除。

代码语言:javascript
复制
type FieldNode struct {
        NodeType
        Pos

        Ident []string // The identifiers in lexical order.
        // contains filtered or unexported fields
}

func (*FieldNode) CopySource

代码语言:javascript
复制
func (f *FieldNode) Copy() Node

func (*FieldNode) StringSource

代码语言:javascript
复制
func (f *FieldNode) String() string

键入IdentifierNodeSource

IdentifierNode 包含一个标识符。

代码语言:javascript
复制
type IdentifierNode struct {
        NodeType
        Pos

        Ident string // The identifier's name.
        // contains filtered or unexported fields
}

func NewIdentifierSource

代码语言:javascript
复制
func NewIdentifier(ident string) *IdentifierNode

NewIdentifier 用给定的标识符名称返回一个新的 IdentifierNode。

func (*IdentifierNode) CopySource

代码语言:javascript
复制
func (i *IdentifierNode) Copy() Node

func (*IdentifierNode) SetPosSource

代码语言:javascript
复制
func (i *IdentifierNode) SetPos(pos Pos) *IdentifierNode

SetPos 设置位置。NewIdentifier 是一种公共方法,所以我们不能修改它的签名。为了方便链接。TODO:修好一天?

func (*IdentifierNode) SetTreeSource

代码语言:javascript
复制
func (i *IdentifierNode) SetTree(t *Tree) *IdentifierNode

SetTree 设置节点的父树。NewIdentifier 是一种公共方法,所以我们不能修改它的签名。为了方便链接。TODO:修好一天?

func (*IdentifierNode) StringSource

代码语言:javascript
复制
func (i *IdentifierNode) String() string

type IfNodeSource

IfNode 表示一个 {{if}} 动作及其命令。

代码语言:javascript
复制
type IfNode struct {
        BranchNode
}

func (*IfNode) CopySource

代码语言:javascript
复制
func (i *IfNode) Copy() Node

type ListNodeSource

ListNode 包含一系列节点。

代码语言:javascript
复制
type ListNode struct {
        NodeType
        Pos

        Nodes []Node // The element nodes in lexical order.
        // contains filtered or unexported fields
}

func (*ListNode) CopySource

代码语言:javascript
复制
func (l *ListNode) Copy() Node

func (*ListNode) CopyListSource

代码语言:javascript
复制
func (l *ListNode) CopyList() *ListNode

func (*ListNode) StringSource

代码语言:javascript
复制
func (l *ListNode) String() string

type NilNodeSource

NilNode 保存表示无类型 nil 常量的特殊标识符'nil'。

代码语言:javascript
复制
type NilNode struct {
        NodeType
        Pos
        // contains filtered or unexported fields
}

func (*NilNode) CopySource

代码语言:javascript
复制
func (n *NilNode) Copy() Node

func (*NilNode) StringSource

代码语言:javascript
复制
func (n *NilNode) String() string

func (*NilNode) TypeSource

代码语言:javascript
复制
func (n *NilNode) Type() NodeType

type NodeSource

节点是分析树中的一个元素。界面很简单。该接口包含一个未导出的方法,因此只有该包的本地类型才能满足它。

代码语言:javascript
复制
type Node interface {
        Type() NodeType
        String() string
        // Copy does a deep copy of the Node and all its components.
        // To avoid type assertions, some XxxNodes also have specialized
        // CopyXxx methods that return *XxxNode.
        Copy() Node
        Position() Pos // byte position of start of node in full original input string
        // contains filtered or unexported methods
}

键入NodeTypeSource

NodeType 标识分析树节点的类型。

代码语言:javascript
复制
type NodeType int
代码语言:javascript
复制
const (
        NodeText    NodeType = iota // Plain text.
        NodeAction                  // A non-control action such as a field evaluation.
        NodeBool                    // A boolean constant.
        NodeChain                   // A sequence of field accesses.
        NodeCommand                 // An element of a pipeline.
        NodeDot                     // The cursor, dot.

        NodeField      // A field or method name.
        NodeIdentifier // An identifier; always a function name.
        NodeIf         // An if action.
        NodeList       // A list of Nodes.
        NodeNil        // An untyped nil constant.
        NodeNumber     // A numerical constant.
        NodePipe       // A pipeline of commands.
        NodeRange      // A range action.
        NodeString     // A string constant.
        NodeTemplate   // A template invocation action.
        NodeVariable   // A $ variable.
        NodeWith       // A with action.
)

func (NodeType) TypeSource

代码语言:javascript
复制
func (t NodeType) Type() NodeType

Type 返回自身并提供一个简单的默认实现来嵌入节点。嵌入在所有非平凡的节点中。

键入NumberNodeSource

NumberNode 包含一个数字:有符号或无符号整数,浮点数或复数。该值被解析并存储在所有可以表示该值的类型下。这在少量代码中模拟 Go 的理想常量的行为。

代码语言:javascript
复制
type NumberNode struct {
        NodeType
        Pos

        IsInt      bool       // Number has an integral value.
        IsUint     bool       // Number has an unsigned integral value.
        IsFloat    bool       // Number has a floating-point value.
        IsComplex  bool       // Number is complex.
        Int64      int64      // The signed integer value.
        Uint64     uint64     // The unsigned integer value.
        Float64    float64    // The floating-point value.
        Complex128 complex128 // The complex value.
        Text       string     // The original textual representation from the input.
        // contains filtered or unexported fields
}

func (*NumberNode) CopySource

代码语言:javascript
复制
func (n *NumberNode) Copy() Node

func (*NumberNode) StringSource

代码语言:javascript
复制
func (n *NumberNode) String() string

键入PipeNodeSource

PipeNode 拥有一个可选声明的管道

代码语言:javascript
复制
type PipeNode struct {
        NodeType
        Pos

        Line int             // The line number in the input. Deprecated: Kept for compatibility.
        Decl []*VariableNode // Variable declarations in lexical order.
        Cmds []*CommandNode  // The commands in lexical order.
        // contains filtered or unexported fields
}

func (*PipeNode) CopySource

代码语言:javascript
复制
func (p *PipeNode) Copy() Node

func (*PipeNode) CopyPipeSource

代码语言:javascript
复制
func (p *PipeNode) CopyPipe() *PipeNode

func (*PipeNode) StringSource

代码语言:javascript
复制
func (p *PipeNode) String() string

键入PosSource

Pos 表示从中解析模板的原始输入文本中的字节位置。

代码语言:javascript
复制
type Pos int

func (Pos) PositionSource

代码语言:javascript
复制
func (p Pos) Position() Pos

键入RangeNodeSource

RangeNode表示一个 {{range}} 动作及其命令。

代码语言:javascript
复制
type RangeNode struct {
        BranchNode
}

func (*RangeNode) CopySource

代码语言:javascript
复制
func (r *RangeNode) Copy() Node

键入StringNodeSource

StringNode 包含一个字符串常量。该值已被“未引用”。

代码语言:javascript
复制
type StringNode struct {
        NodeType
        Pos

        Quoted string // The original text of the string, with quotes.
        Text   string // The string, after quote processing.
        // contains filtered or unexported fields
}

func (*StringNode) CopySource

代码语言:javascript
复制
func (s *StringNode) Copy() Node

func (*StringNode) StringSource

代码语言:javascript
复制
func (s *StringNode) String() string

键入TemplateNodeSource

TemplateNode 表示一个 {{template}} 动作。

代码语言:javascript
复制
type TemplateNode struct {
        NodeType
        Pos

        Line int       // The line number in the input. Deprecated: Kept for compatibility.
        Name string    // The name of the template (unquoted).
        Pipe *PipeNode // The command to evaluate as dot for the template.
        // contains filtered or unexported fields
}

func (*TemplateNode) CopySource

代码语言:javascript
复制
func (t *TemplateNode) Copy() Node

func (*TemplateNode) StringSource

代码语言:javascript
复制
func (t *TemplateNode) String() string

键入TextNodeSource

TextNode 保存纯文本。

代码语言:javascript
复制
type TextNode struct {
        NodeType
        Pos

        Text []byte // The text; may span newlines.
        // contains filtered or unexported fields
}

func (*TextNode) CopySource

代码语言:javascript
复制
func (t *TextNode) Copy() Node

func (*TextNode) StringSource

代码语言:javascript
复制
func (t *TextNode) String() string

键入TreeSource

树是单个解析模板的表示。

代码语言:javascript
复制
type Tree struct {
        Name      string    // name of the template represented by the tree.
        ParseName string    // name of the top-level template during parsing, for error messages.
        Root      *ListNode // top-level root of the tree.
        // contains filtered or unexported fields
}

func NewSource

代码语言:javascript
复制
func New(name string, funcs ...map[string]interface{}) *Tree

New 分配给定名称的新分析树。

func (*Tree) CopySource

代码语言:javascript
复制
func (t *Tree) Copy() *Tree

Copy 返回树的副本。任何解析状态都会被丢弃。

func (*Tree) ErrorContextSource

代码语言:javascript
复制
func (t *Tree) ErrorContext(n Node) (location, context string)

ErrorContext 返回输入文本中节点位置的文本表示。只有当节点没有指向内部树的指针时才会使用接收器,这可能会发生在旧代码中。

func (*Tree) ParseSource

代码语言:javascript
复制
func (t *Tree) Parse(text, leftDelim, rightDelim string, treeSet map[string]*Tree, funcs ...map[string]interface{}) (tree *Tree, err error)

Parse 解析模板定义字符串以构建用于执行的模板表示。如果任何动作分隔符字符串为空,则使用默认值(“{{”或“}}”)。嵌入式模板定义被添加到树形图。

键入VariableNodeSource

VariableNode 包含变量名称列表,可能带有链接字段访问。美元符号是(第一)名称的一部分。

代码语言:javascript
复制
type VariableNode struct {
        NodeType
        Pos

        Ident []string // Variable name and fields in lexical order.
        // contains filtered or unexported fields
}

func (*VariableNode) CopySource

代码语言:javascript
复制
func (v *VariableNode) Copy() Node

func (*VariableNode) StringSource

代码语言:javascript
复制
func (v *VariableNode) String() string

键入WithNodeSource

WithNode 表示一个 {{with}} 动作及其命令。

代码语言:javascript
复制
type WithNode struct {
        BranchNode
}

func (*WithNode) CopySource

代码语言:javascript
复制
func (w *WithNode) Copy() Node

扫码关注腾讯云开发者

领取腾讯云代金券