pub struct Template { /* private fields */ }
Expand description
Represents a MediaWiki template ({{foo}}
)
How to access values from an existing template:
let code = client.transform_to_html("{{1x|test}}").await?.into_mutable();
// Get the `Template` instance
let template = code.filter_templates()?[0].clone();
assert_eq!(template.name(), "Template:1x".to_string());
assert_eq!(template.raw_name(), "./Template:1x".to_string());
assert_eq!(template.name_in_wikitext(), "1x".to_string());
assert_eq!(template.param("1"), Some("test".to_string()));
How to create and insert a new template:
let mut params = map::IndexMap::new();
params.insert("1".to_string(), "test".to_string());
let template = Template::new("1x", ¶ms)?;
let code = Wikicode::new("");
code.append(&template);
let wikitext = client.transform_to_wikitext(&code).await?;
assert_eq!(wikitext, "{{1x|test}}".to_string());
You can also use Template::new_simple()
if there are no parameters to pass.
Implementations§
Source§impl Template
impl Template
Sourcepub fn new_simple(name: &str) -> Self
pub fn new_simple(name: &str) -> Self
Create a new template with no parameters
Sourcepub fn new(name: &str, params: &IndexMap<String, String>) -> Result<Self>
pub fn new(name: &str, params: &IndexMap<String, String>) -> Result<Self>
Create a new template
Sourcepub fn remove_param(&self, name: &str) -> Result<Option<String>>
pub fn remove_param(&self, name: &str) -> Result<Option<String>>
Remove the specified parameter from the template. If it was set, the previous value will be returned.
Sourcepub fn set_param(&self, name: &str, wikitext: &str) -> Result<Option<String>>
pub fn set_param(&self, name: &str, wikitext: &str) -> Result<Option<String>>
Set the specified wikitext as a template parameter with the given name
The previous value, if one was set, will be returned.
Sourcepub fn set_params(
&self,
map: impl Into<IndexMap<String, String>>,
) -> Result<IndexMap<String, String>>
pub fn set_params( &self, map: impl Into<IndexMap<String, String>>, ) -> Result<IndexMap<String, String>>
Override all parameters with the specified map. A map of all previously set parameters and their values will be returned.
Sourcepub fn name_in_wikitext(&self) -> String
pub fn name_in_wikitext(&self) -> String
Get the name of the template as it appears in wikitext
Sourcepub fn set_name(&self, name: String) -> Result<()>
pub fn set_name(&self, name: String) -> Result<()>
Change the name of the template or parser function.
Note that the name
should be how the name in wikitext.
For example, "1x"
for a Template:1x,
Sourcepub fn raw_name(&self) -> String
pub fn raw_name(&self) -> String
Get the full name of the template, e.g. ./Template:Foo_bar
or
the parser function, e.g. ifeq
.
Sourcepub fn name(&self) -> String
pub fn name(&self) -> String
Get a pretty normalized name of a template, e.g. Template:Foo bar
or
the parser function, e.g. ifeq
Sourcepub fn params(&self) -> IndexMap<String, String>
pub fn params(&self) -> IndexMap<String, String>
Get a map of all parameters, named and unnamed
Sourcepub fn param(&self, name: &str) -> Option<String>
pub fn param(&self, name: &str) -> Option<String>
Get the wikitext value of a specific parameter if it exists
Sourcepub fn param_in_wikitext(&self, name: &str) -> Option<String>
pub fn param_in_wikitext(&self, name: &str) -> Option<String>
Get the name of the parameter as it appears in the wikitext. For
example given {{1x|param<!--comment-->name=value}}
looking up
paramname
would return Some("param<!--comment->name")
.
Sourcepub fn is_template(&self) -> bool
pub fn is_template(&self) -> bool
Whether it’s a template (as opposed to a parser function)
Sourcepub fn is_parser_function(&self) -> bool
pub fn is_parser_function(&self) -> bool
Whether it’s a parser function (as opposed to a template)