How to use template_any_ID() into Ui Layout?
Look at the code
With any blender layout, try grepping the bl_ui code to see if it has been used
and will see this has been used in the scene properties panel.
 bl_ui/properties_scene.py
class SCENE_PT_keying_set_paths(SceneButtonsPanel, SceneKeyingSetsPanel, Panel):
    bl_label = &quot;Active Keying Set&quot;
    bl_parent_id = &quot;SCENE_PT_keying_sets&quot;
    ...

    scene = context.scene
    ks = scene.keying_sets.active

    ...


    ksp = ks.paths.active
    if ksp:
        col = flow.column(align=True)
        col.alignment = 'RIGHT'

        col.template_any_ID(ksp, &quot;id&quot;, &quot;id_type&quot;, text=&quot;Target ID-Block&quot;)


Target ID Block: where I've selected camera
and will see that this is not unlike a driver variable target, in that it lets us set the ID type of object, before selecting an object of that id_type
&gt;&gt;&gt; ks = C.scene.keying_sets.active
&gt;&gt;&gt; ksp = ks.paths.active
&gt;&gt;&gt; ksp.id
bpy.data.objects['Camera']

&gt;&gt;&gt; ksp.id_type
'OBJECT'

or alternatively to the camera data part
&gt;&gt;&gt; ksp.id
bpy.data.cameras['Camera']

&gt;&gt;&gt; ksp.id_type
'CAMERA'

ie we are setting a pointer to object  ksp.id to &quot;any type&quot; based on the id_type.