Creating a custom productlist
Instead of just placing one product-template in the document, it is possible to place any combination of templates in a document. This might be needed when the main product as well as its variants / associated products should be displayed with different layouts. It might also make sense to split up a complex product-layout into multiple "sub"-templates to make the layout as dynamic as possible. For this, the productlist that is used as parameter for comet.product.establish can be assembled by any custom logic.
Create a productlist by a custom logic
To create a product for a product-list, the Python-method comet.product.create can be used. Make sure to specify the correct Id and attributes for the type of product you want to create.
For Product-templates, an Id is needed, that matches this structure: (1,0,0,'string-id').
For inserting a page with a pagetemplate, no string-id is needed, but make sure to specify the correct product-type (see comet documentation for reference).
Two functions from the templates.py-library can be used to simplify this process:
Examples:
templates_lib = comet.pyImport("[xml]/library/framework/templates.py")
template_id_product = 1
pagetemplate_id_product = 5
template_id_variant = 2
pagetemplate_id_variant = 6
productlist = [
templates_lib.create_comet_page(pagetemplate_id_product),
templates_lib.create_comet_product("product_id_of_xml_node", template_id_product),
templates_lib.create_comet_page(pagetemplate_id_variant),
templates_lib.create_comet_product("variant_id_of_xml_node", template_id_variant)
]
templates_lib = comet.pyImport("[xml]/library/framework/templates.py")
pagetemplate_id_product: int = int(DATAPROVIDER.get_attribute_value("pagetemplate_id"))
template_id_product: int = int(DATAPROVIDER.get_attribute_value("template_id"))
productlist = [
templates_lib.create_comet_page(pagetemplate_id_product),
templates_lib.create_comet_product("product_id_of_xml_node", template_id_product)
]
templates_lib = comet.pyImport("[xml]/library/framework/templates.py")
pagetemplate = 10
template_product_header = 1
template_product_features = 2
template_product_description = 3
pagetemplate_image_page = 11
template_image = 4
pagetemplate_accessories = 12
template_accessory = 5
productlist = [
templates_lib.create_comet_page(pagetemplate),
templates_lib.create_comet_product("product_id_of_xml_node", template_product_header),
templates_lib.create_comet_product("product_id_of_xml_node", template_product_features),
templates_lib.create_comet_product("product_id_of_xml_node", template_product_description)
]
asset_ids: list[str] = DATAPROVIDER.get_attribute_values("assets")
if asset_ids:
productlist.append(templates_lib.create_comet_page(pagetemplate_image_page))
for asset_id in asset_ids:
productlist.append(templates_lib.create_comet_product(asset_id, template_image))
accessory_products: list[str] = DATAPROVIDER.get_attribute_values("accessories")
if accessory_products:
productlist.append(templates_lib.create_comet_page(pagetemplate_accessories))
for accessory_id in accessory_products:
productlist.append(templates_lib.create_comet_product(accessory_id, template_accessory))
For complex layout-logic it might be helpful to move the logic for creating the productlist to a custom library.
custom_library = comet.pyImport("[xml]/library/extension/custom_library.py")
productlist = custom_library.create_productlist()
Retrieve template-information from the datafile
The default-project offers many ways to retrieve template-information (specifically: pagetemplate-id, parent-page, product-template-id) from the incoming rendering-request and predefined structures in the project. For example hardcoded ids in constants.py at a mapping-key that could be given in xml-data or a renderpdf-request-parameter.
Here are some practical examples of such a function:
Default Mapping
Retrieve values from TEMPLATEMAPPING (type: dict) in constants.py at the key "default"
templates_lib = comet.pyImport("[xml]/library/framework/templates.py")
my_datasource_lib = comet.pyImport('[xml]/library/framework/data_provider.py')
DATAPROVIDER = my_datasource_lib.DataProvider()
mapping = templates_lib.TemplateMapping()
PAGE_TEMPLATE_ID, PARENT_PAGE, TEMPLATE_ID = mapping.get_mapping_ids()
productlist = mapping.get_productlist([STRING_ID])
Custom Mapping
Retrieve values from TEMPLATEMAPPING (type: dict) in constants.py at the key "default"
templates_lib = comet.pyImport("[xml]/library/framework/templates.py")
my_datasource_lib = comet.pyImport('[xml]/library/framework/data_provider.py')
DATAPROVIDER = my_datasource_lib.DataProvider()
mapping = templates_lib.TemplateMapping(templates_lib.TemplateMapping.Method.BY_KEY, mapping_key="demo")
PAGE_TEMPLATE_ID, PARENT_PAGE, TEMPLATE_ID = mapping.get_mapping_ids()
productlist = mapping.get_productlist([STRING_ID])
Mapping by key from default-key in XML-data
Retrieve values from TEMPLATEMAPPING (type: dict) in constants.py at a custom key. Key is passed in xml-payload at attribute "templatemapping"
<data>
<item type="product" id="123">
<item key="templatemapping" content="my_template_mapping"/>
</item>
</data>
templates_lib = comet.pyImport("[xml]/library/framework/templates.py")
my_datasource_lib = comet.pyImport('[xml]/library/framework/data_provider.py')
DATAPROVIDER = my_datasource_lib.DataProvider()
mapping = templates_lib.TemplateMapping(templates_lib.TemplateMapping.Method.BY_KEY_XMLDATA, DATAPROVIDER)
PAGE_TEMPLATE_ID, PARENT_PAGE, TEMPLATE_ID = mapping.get_mapping_ids()
productlist = mapping.get_productlist([STRING_ID])
Mapping by key from default request-parameter
Retrieve values from TEMPLATEMAPPING (type: dict) in constants.py at a custom key. Key is passed as url-parameter "templatemapping" of renderpdf-request (written to environment-variables by same key)
(...)/renderpdf?custom=templatemapping:my_template_mapping
templates_lib = comet.pyImport("[xml]/library/framework/templates.py")
my_datasource_lib = comet.pyImport('[xml]/library/framework/data_provider.py')
DATAPROVIDER = my_datasource_lib.DataProvider()
mapping = templates_lib.TemplateMapping(templates_lib.TemplateMapping.Method.BY_KEY_REQUESTPARAM)
PAGE_TEMPLATE_ID, PARENT_PAGE, TEMPLATE_ID = mapping.get_mapping_ids()
productlist = mapping.get_productlist([STRING_ID])
IDs from default-keys in XML-data
Retrieve values from xml-payload at attribute-keys "template_id", "page_template_id", "parent_page"
<data>
<item type="product" id="123">
<item key="template_id" content="654"/>
<item key="page_template_id" content="1"/>
<item key="parent_page" content="A-Page"/>
</item>
</data>
templates_lib = comet.pyImport("[xml]/library/framework/templates.py")
my_datasource_lib = comet.pyImport('[xml]/library/framework/data_provider.py')
DATAPROVIDER = my_datasource_lib.DataProvider()
mapping = templates_lib.TemplateMapping(templates_lib.TemplateMapping.Method.BY_IDS_XMLDATA, DATAPROVIDER)
PAGE_TEMPLATE_ID, PARENT_PAGE, TEMPLATE_ID = mapping.get_mapping_ids()
productlist = mapping.get_productlist([STRING_ID])
Productlist from mapping by key from custom request-parameter
Create a productlist automatically by a mapping predefined in constants.py, selected by a key (for example sent in a request-parameter):
(...)/renderpdf?custom=some_custom_parameter:my_template_mapping
templatemapping_key = PRIINTCLOUD.get_cloud_environment().get("some_custom_parameter","")
mapping = templates_lib.TemplateMapping(templates_lib.TemplateMapping.Method.BY_KEY, mapping_key=templatemapping_key)
PAGE_TEMPLATE_ID, PARENT_PAGE, TEMPLATE_ID = mapping.get_mapping_ids()
productlist = mapping.get_productlist([STRING_ID])